首页 > 代码库 > POJ 1458 - Common Subsequence(最长公共子串)

POJ 1458 - Common Subsequence(最长公共子串)

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

题目链接:http://poj.org/problem?id=1458

 

 

AC代码:

 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 using namespace std; 7 int ans,lena,lenb,f[201][201]; 8 //f[i][j]表示a匹配到i-1,b匹配到j-1  9 char a[210],b[210];10 int main()11 {12     while(~scanf("%s%s",a,b))13     {//特殊的读入技巧(不 14         memset(f,0,sizeof(f));15         lena = strlen(a);16         lenb = strlen(b);17         for(int i = 1;i <= lena;++ i)18         {19             for(int j = 1;j <= lenb;++ j)20             {21                 if(a[i-1] == b[j-1])22                     f[i][j] = f[i-1][j-1] + 1;23                 else24                     f[i][j] = max(f[i-1][j],f[i][j-1]);25             }26         }27         printf("%d\n",f[lena][lenb]);28     }29     return 0;30 }

 

POJ 1458 - Common Subsequence(最长公共子串)