void printLCS(int x,int y) { if(!x || !y ) return ; if(path[x][y]=='C') { printLCS(x-1,y-1); cout<<(char)s[x-1]; } else if(path[x][y]=='U') { printLCS(x-1,y); } else printLCS(x,y-1); }
void printLCS(char i, char j){ if(i == 0 || j == 0) return; if(mov[i][j]){ if(mov[i][j]&1) printLCS(i,j-1); else printLCS(i-1,j); } else { printLCS(i-1,j-1); if(!first) printf(" %s",w_first[i-1]); else first = 0, printf("%s",w_first[i-1]); } }
int LCS_DP(char *a, char* b, int m, int n){ int i,j; for(i=0;i<=m;i++){ for(j=0;j<=n;j++){ if(i==0||j==0) { LCS[i][j] = 0; } else if(*(a+i-1)== *(b+j-1)) { LCS[i][j] = LCS[i-1][j-1]+1; } else{ LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1]); } } } printf("LCS String :: ") printLCS(a,b,m,n); return LCS[m][n]; }
void main() { string s1, s2; cout << "Enter the first string: "; getline(cin, s1); cout << "Enter the second string: "; getline(cin, s2); printLCS(lcsLength(s1, s2), s1, s1.length(), s2.length()); cout << endl; }
int main(void) { printf("\nДължина на най-дългата обща подредица: %u", LCS_Length()); printf("\nPrintLCS: Максимална обща подредица (в обратен ред): "); printLCS(); printf("\nPrintLCS2: Максимална обща подредица: "); printLCS2(strlen(x), strlen(y)); printf("\nPrintLCS3: Максимална обща подредица: "); printLCS3(strlen(x), strlen(y)); return 0; }
void printLCS(std::vector<char> * result, const std::vector<char>& a, std::vector<std::vector<int> >& c, std::vector<std::vector<int> >& lcstrack, int i, int j) { if(i <= 0 || j <= 0) { return; } if(lcstrack[i][j] == 0) { result->push_back(a.at(i - 1)); printLCS(result, a, c, lcstrack, i - 1, j - 1); } else if(lcstrack[i][j] == 1) { printLCS(result, a, c, lcstrack, i-1, j); } else { printLCS(result, a, c, lcstrack, i, j-1); } }
void printLCS(int i,int j) { if (i==0 || j==0) return; if (b[i][j]==1) { printLCS(i-1,j-1); if(i-1 ==0|| j-1 ==0) { printf("%s",X[i-1].seq); } else { printf(" %s",X[i-1].seq); } } else if (b[i][j]==2) printLCS(i-1,j); else printLCS(i,j-1); }
void printLCS(const char *str1, const char *str2, const int DP[][MAX], const int preInfo[][MAX], const int i, const int j, char lcs[], int lcsSize) { if (i >= 0 && j >= 0) { if (preInfo[i][j] & INCLUDE_ME) { lcs[lcsSize] = str1[i]; printLCS(str1, str2, DP, preInfo, i-1, j-1, lcs, lcsSize+1); } if (preInfo[i][j] & SAME_AS_LEFT) { printLCS(str1, str2, DP, preInfo, i, j-1, lcs, lcsSize); } if (preInfo[i][j] & SAME_AS_UP) { printLCS(str1, str2, DP, preInfo, i-1, j, lcs, lcsSize); } } else { int k; for (k = lcsSize-1; k >= 0; k--) { printf("%c ", lcs[k]); } printf("\n"); } }
void main() { while(true){ char c; int i=0; int word1 =0; int word2 =0; while((c = getchar())!=EOF) { if(c>='a' && c<='z') { X[word1].seq[i++] = c; } else if(c == ' '|| c=='\n' && i!=0) { X[word1].seq[i]='\0';word1++;i=0; } else if(c =='#'){ X[word1].seq[i]='\0';i=0; break; } } if(c == EOF) break; while((c = getchar())!=EOF) { if(c>='a' && c<='z') { Y[word2].seq[i++] = c; } else if(c == ' '|| c=='\n' && i!=0) { Y[word2].seq[i]='\0';word2++;i=0; } else if(c =='#'){ Y[word2].seq[i]='\0';i=0; break; } } LCSlength(word1,word2); printLCS(word1,word2); printf("\n"); if(c == EOF) break; } }
void LCS(const char *str1, const size_t length1, const char *str2, const size_t length2) { int DP[MAX][MAX] = {0}; int preInfo[MAX][MAX] = {HEAD}; int i, j; for (i = 0; i < length1; i++) { for (j = 0; j < length2; j++) { if (str1[i] == str2[j]) { if (i == 0 || j == 0) { DP[i][j] = 1; } else { DP[i][j] = DP[i-1][j-1] + 1; } preInfo[i][j] = INCLUDE_ME; } else { if (i == 0 && j == 0) { DP[i][j] = 0; } else if (i > 0 && j == 0) { DP[i][j] = DP[i-1][j]; preInfo[i][j] = SAME_AS_UP; } else if (i == 0 && j > 0) { DP[i][j] = DP[i][j-1]; preInfo[i][j] = SAME_AS_LEFT; } else { int left = DP[i][j-1]; int up = DP[i-1][j]; if (left > up) { DP[i][j] = left; preInfo[i][j] = SAME_AS_LEFT; } else if (left == up) { DP[i][j] = left; preInfo[i][j] = SAME_AS_LEFT | SAME_AS_UP; } else { DP[i][j] = up; preInfo[i][j] = SAME_AS_UP; } } } } } char lcs[MAX]; printLCS(str1, str2, DP, preInfo, length1-1, length2-1, lcs, 0); }
std::vector<char> * getLongestCommonSubsequence(std::vector<char>& a, std::vector<char>& b) { std::vector<std::vector<int> > c; std::vector<std::vector<int> > lcstrack; std::vector<char> * result = new std::vector<char>; c.resize(a.size() + 1); lcstrack.resize(a.size() + 1); for(int i = 0;i <= a.size();i++) { c[i].resize(b.size() + 1); lcstrack[i].resize(b.size() + 1); c[i][0] = 0; } for(int j = 0;j <=b.size();j++) { c[0][j] = 0; } for(int i = 1;i <= a.size();i++) { for(int j = 1;j <= b.size();j++) { if(a.at(i - 1) == b.at(j - 1)) { c[i][j] = c[i - 1][j - 1] + 1; lcstrack[i][j] = 0; } else if(c[i - 1][j] > c[i][j - 1]) { c[i][j] = c[i - 1][j]; lcstrack[i][j] = 1; } else { c[i][j] = c[i][j - 1]; lcstrack[i][j] = 2; } } } printLCS(result, a, c, lcstrack, a.size(), b.size()); std::reverse(result->begin(), result->end()); return result; }
void LCS(int firststring_length,int secondstring_length,int **LCSarray,char *firststring,char *secondstring){ //populates LCSarray and solutionarray used in //finding subsequence int i,j; char **solutionarray=createchararray(firststring_length,secondstring_length); //stores directions for traversal int n=0; for(i=0;i<=firststring_length;i++){ LCSarray[i][0]=0; solutionarray[i][0]='0'; } for(j=0;j<=secondstring_length;j++){ LCSarray[0][j]=0; solutionarray[0][j]='0'; } for(i=1;i<=firststring_length;i++){ for(j=1;j<=secondstring_length;j++){ if(firststring[i-1]==secondstring[j-1]){ LCSarray[i][j]=LCSarray[i-1][j-1]+1; solutionarray[i][j]='D'; } else{ if(LCSarray[i-1][j] >= LCSarray[i][j-1]){ LCSarray[i][j]=LCSarray[i-1][j]; solutionarray[i][j]='U'; } else{ LCSarray[i][j]=LCSarray[i][j-1]; solutionarray[i][j]='L'; } } } } printLCS(firststring_length,secondstring_length,solutionarray,firststring,LCSarray); free(solutionarray); }
int main(void){ while(scanf("%s",&w_first[0])!=EOF){ m = 1; n = 0; while(scanf("%s",&w_first[m++]) && w_first[m-1][0] != '#'); while(scanf("%s",&w_sec[n++]) && w_sec[n-1][0] != '#'); for(i = 0; i < m; i++) lcs[i][0] = 0; for(j = 0; j < n; j++) lcs[0][j] = 0; for(i = 1; i < m; i++){ for(j = 1; j < n; j++) if(strcmp(w_first[i-1],w_sec[j-1])==0) lcs[i][j] = lcs[i-1][j-1]+1, mov[i][j] = 0; else if(lcs[i][j-1]>lcs[i-1][j]) lcs[i][j] = lcs[i][j-1], mov[i][j] = 1; else lcs[i][j] = lcs[i-1][j], mov[i][j] = 2; } first = 1; printLCS(m-1,n-1); putchar('\n'); } return 0; }
inline LCSprinter::LCSprinter(const QStringList& s_1, const QStringList& s_2, QVector<LCSMarker> *b_, const uint nT_, uint index, const QStringList& s1Space_, const QStringList& s2Space_ ) : s1(s_1) , s2(s_2) , s1Space(s1Space_) , s2Space(s2Space_) , it1(s1.constBegin()) , it2(s2.constBegin()) , it1Space(s1Space.constBegin()) , it2Space(s2Space.constBegin()) , nT(nT_) , b(b_) { haveSpaces=!s1Space_.isEmpty(); printLCS(index); }
void LCSprinter::printLCS(uint index) { //fprintf(stderr,"%2d. %2d. %2d. %2d\n",(uint)(*b)[index],nT,index%nT, index); if (index % nT == 0 || index < nT) { // original LCS algo does not have to deal with ins before first common uint bound = index%nT; for (index=0; index<bound; ++index) { resultString.append(addMarkerStart); resultString.append(*it2); ++it2; if (haveSpaces) { resultString.append(*it2Space); ++it2Space; } resultString.append(addMarkerEnd); } return; } if (ARROW_UP_LEFT == b->at(index)) { printLCS(index-nT-1); if (it1!=s1.constEnd()) { //kWarning() << "upleft '" << *it1 <<"'"; //kWarning() << "upleft 1s" << *it1Space; //kWarning() << "upleft 2s" << *it2Space; if (haveSpaces) { if((*it1)==(*it2))//case and accels resultString.append(*it1); else { QStringList word1=prepareForInternalDiff(*it1); QStringList word2=prepareForInternalDiff(*it2); QStringList empty; resultString.append(calcLCS(word1,word2,empty,empty).join(QString())); } if((*it1Space)==(*it2Space)) resultString.append(*it1Space); else { QStringList word1=prepareForInternalDiff(*it1Space); QStringList word2=prepareForInternalDiff(*it2Space); QStringList empty; //empty=calcLCS(word1,word2,empty,empty); //???this is not really good if we use diff result in autosubst empty=calcLCS(word2,word1,empty,empty); empty.replaceInStrings("KBABELADD>","KBABELTMP>"); empty.replaceInStrings("KBABELDEL>","KBABELADD>"); empty.replaceInStrings("KBABELTMP>","KBABELDEL>"); resultString.append(empty.join(QString())); } ++it1Space; ++it2Space; //kWarning() << " common " << *it1; } else resultString.append(*it1);//we may guess that this is a batch job, i.e. TM search ++it1; ++it2; } } else if (ARROW_UP == b->at(index)) { printLCS(index-nT); // if (it1!=s1.end()) { //kWarning()<<"APPENDDEL "<<*it1; //kWarning()<<"APPENDDEL "<<*it1Space; resultString.append(delMarkerStart); resultString.append(*it1); ++it1; if (haveSpaces) { resultString.append(*it1Space); ++it1Space; } resultString.append(delMarkerEnd); } } else { printLCS(index-1); resultString.append(addMarkerStart); resultString.append(*it2); ++it2; if (haveSpaces) { //kWarning() << "add2 " << *it2; resultString.append(*it2Space); ++it2Space; } resultString.append(addMarkerEnd); } }