locked
Longest Common Substring RRS feed

  • Question

  • Hi all,

    I need a LCS code with MPI. I have the code in C++, but don't know how to implement MPI in the code. Can someone help me?

    Thanks.


    Here is the code:


    #include <iostream>
    #include <string>

    using namespace std;
    int main () {
       
        string first, second, lcsub, max;
       
        cout << "Enter two words" << endl;
        cin >> first >> second;
            for (int i=0; i < first.length(); i++)
                for (int j=0; j < second.length(); j++)
                    for (int k=1; k <= first.length() && k <= second.length(); k++){
                        if (first.substr(i,k) == second.substr(j,k)){
                            lcsub = first.substr(i,k);
                        }
                        else{
                            if (lcsub.length() > max.length())
                                max=lcsub;
                            lcsub="";
                        }
                    }
                        if (lcsub.length() > max.length())
                                max=lcsub;
                            lcsub="";
                            cout << "Longest Common Substring: " << max << endl;
        return 0;
    }
    Thursday, February 11, 2010 2:07 AM

Answers

All replies

  • Hi,

    The following may be interested to you for MPI programming.
    http://www.mcs.anl.gov/research/projects/mpi/usingmpi/

    It has rich MPI tutorial and good examples.

    Liwei
    Thursday, February 11, 2010 9:14 PM
  • Thanks a lot for the link, I have tutorials, but we learned only the basic things about MPI and I haven't enough knowledge to solve the problem :(. I have to finish the project until February 15, so if anyone can help me, I will be very glad.  

    I tried something but i don't know the next steps....

    #include <iostream>
    #include <string>
    #include "mpi.h"
    
    using namespace std;
    
    int main (int argc, char *argv[]) 
    {
        MPI::Init(argc,argv);	
        
        int id,p;
        string first, second, lcsub, max;
        id = MPI::COMM_WORLD.Get_rank();
        p = MPI::COMM_WORLD.Get_size(); 
        
        cout << "Enter two words" << endl;
        cin >> first >> second;
            for (int i=0; i < first.length(); i++)
                for (int j=0; j < second.length(); j++)
                    for (int k=1; k <= first.length() && k <second.length(); k++){
                        if (first.substr(i,k) == second.substr(j,k)){
                            lcsub = first.substr(i,k);
                        }
                        else{
                            if (lcsub.length() > max.length())
                                max=lcsub;
                            lcsub="";
                        }
                    }
                        if (lcsub.length() > max.length())
                                max=lcsub;
                            lcsub="";
                            cout << "Longest Common Substring: " << max << endl;
        MPI::Finalize(); 
        return 0;
    }
    
    I don't know how correct it is, and how to implement Send, Recieve or Broadcast functions...

    MPI_Bcast(max, max.length(), MPI::CHAR, root, MPI::COMM_WORLD);

    THANKS!




    Friday, February 12, 2010 12:42 AM