Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

brute force example for LCS

Revision 1 vs. Revision 2

Legend:

Unmodified
Added
Removed
  • Code

    r1 r2  
    1111        << "First we proceed by going through X compared to Y and then " 
    1212        "going through comparing Y to X.  In either case we get 'a' not 'the' " 
    1313        "longest subsequence." << std::endl << std::endl; 
    1414        std::vector<std::string> vs; 
    1515        std::string x="ABCDBAGZCDCA"; 
    16         std::string y="BCADCGZ"; 
     16        //std::string y="BCADCGZ"; 
     17        std::string y="bcadcgz"; 
     18        //std::string x="ABCDE"; 
     19        //std::string y="abcde"; 
    1720        size_t n=x.size(); 
    1821        size_t m=y.size(); 
    1922        size_t i(0), j(0), ii(0), jj(0); 
    2023        std::string ts; 
    2124        std::string longest; 
     25        size_t count=0; 
    2226        for (; i < n; ++i ) { 
    2327                ii = i; 
    2428                j = 0; 
    2529                jj = j; 
    2630                ts.clear(); 
    2731                do { 
     32                        count++; 
    2833                        if (x[ii] == y[j]) { 
    2934                                std::cout << x[ii]; 
    3035                                ts += x[ii]; 
     36                                if ( ii == n-1 && j == 0) break; 
    3137                                ++ii; 
    3238                                jj=++j; 
    3339                        } else if ( ++j == m ) { 
    3440                                j = jj; 
    3541                                // wasn't found so next in X 
    3642                                ++ii; 
     43                                if ( j == 0 && ii == n ) break; 
    3744                        } 
    3845                         
    3946                } while ( ii < n && jj < m ); 
    4047                std::cout << std::endl; 
    4148                if (ts.size() > longest.size()) longest = ts; 
    4249                vs.push_back(ts); 
     50                if ( j == 0 && ii >= n-1 ) break; 
    4351        } 
     52       std::cout << "Actual number of comparisons/looping: " << count << std::endl << std::endl; 
    4453        std::cout << "Longest is: " << longest << std::endl << std::endl; 
    4554        std::cout << std::endl << "Going the other way!" << std::endl; 
    4655        j=0; 
    4756        i=0; 
    4857        jj=0; 
    4958        ii=0; 
    5059        longest.clear(); 
     60        count=0; 
    5161        for (; i < m; ++i ) { 
    5262                ii = i; 
    5363                j = 0; 
    5464                jj = j; 
    5565                ts.clear(); 
    5666                do { 
     67                        count++; 
    5768                        if (y[ii] == x[j]) { 
    5869                                std::cout << y[ii]; 
    5970                                ts+=y[ii]; 
     71                                if ( ii == n-1 && j == 0) break; 
    6072                                ++ii; 
    6173                                jj=++j; 
    6274                        } else if ( ++j == n ) { 
    6375                                j = jj; 
    6476                                // wasn't found so next in Y 
    6577                                ++ii; 
     78                                if ( j == 0 && ii == n) break; 
    6679                        } 
    6780                         
    6881                } while ( ii < m && jj < n ); 
    6982                std::cout << std::endl; 
    7083                vs.push_back(ts); 
    7184                if (ts.size() > longest.size()) longest = ts; 
     85                if ( j == 0 && ii >= n-1 ) break; 
    7286        } 
     87        std::cout << "Actual number of comparisons/looping: " << count << std::endl << std::endl; 
    7388        std::cout << "Longest is: " << longest << std::endl << std::endl; 
    74  
     89        std::cout << " O(n*m) = O(" << n*m << ")" << std::endl;  
    7590        std::cout << "UNIQUE SORTED ORDER: " << std::endl; 
    7691         
    7792        std::sort(vs.begin(), vs.end()); 
    7893        std::vector<std::string>::iterator it = std::unique_copy (vs.begin(), vs.end(), vs.begin()); 
    7994        vs.resize(it - vs.begin());