POJ 1458 - Common Subsequence(最長公共子串)
阿新 • • 發佈:2017-07-18
strlen cstring algorithm 鏈接 space %d ace -s set
此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。
題目鏈接: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-19 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 else 24 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(最長公共子串)