POJ 2264 Advanced Fruits (longest common sub-sequence)

Source: Internet
Author: User

This problem to use the longest common sub-sequence, but also DP, but will not, so to learn this, see for a while, finally have a snack.

The main idea is to first find the longest common subsequence, and then merge all the characters before the common characters. Please see the comments below for details.

#include <cstdio> #include <cstring>const int n=105;char s1[n],s2[n],s[n*2];int Lcs[n][n],index1[n], Index2[n];//index and Index2 are used to record the index where the common characters reside.    void Get_lcs () {int len1=strlen (S1);    int Len2=strlen (s2);    memset (Lcs,0,sizeof (LCS));                 for (int i=1;i<=len1;i++)//To find the length of the longest common subsequence {for (int j=1;j<=len2;j++) {if (s1[i-1]==s2[j-1])            lcs[i][j]=lcs[i-1][j-1]+1;                else {if (lcs[i-1][j]>lcs[i][j-1]) lcs[i][j]=lcs[i-1][j];            else lcs[i][j]=lcs[i][j-1];    }}} int i=len1-1,j=len2-1,top=0;            while (I!=-1&AMP;&AMP;J!=-1)//Find out the index of all common characters {if (S1[i]==s2[j]) {index1[top]=i,index2[top]=j;        Top++,i--, j--;            } else {if (lcs[i+1][j+1]==lcs[i][j]) i--, j--;            else {if (lcs[i+1][j]>lcs[i][j+1]) j--;    else i--; }}} index1[top]=-1,index2[top]=-1;//set this is to merge the characters before the first common character//for (int i=0;i<=top;i++)//printf ("    %d,index1=%d,index2=%d\n ", I,index1[i],index2[i]);    int s_top=0; The characters between for (int i=top;i>=1;i--) {for (int j=index1[i]+1;j<index1[i-1];j++)//Two common characters are merged s[s_top++]=        S1[J];        for (int j=index2[i]+1;j<index2[i-1];j++) S[S_TOP++]=S2[J];    S[s_top++]=s1[index1[i-1]];    } for (int j=index1[0]+1;j<len1;j++)//The last common character after the remaining words conform and s[s_top++]=s1[j]; for (int j=index2[0]+1;j<len2;j++) s[s_top++]=s2[j];}        int main () {while (scanf ("%s%s", S1,s2)!=eof) {for (int i=0;i<2*n;i++) s[i]= ' + ';        Get_lcs ();    printf ("%s\n", s); } return 0;}


POJ 2264 Advanced Fruits (longest common sub-sequence)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.