题解:
裸题,只是为了测试LCS模板写对没有……
1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 2010 4 int lena,lenb,ans1,ans2[MAXN]; 5 char a[MAXN],b[MAXN]; 6 struct node 7 { 8 int x,nextx,nexty; 9 }f[MAXN][MAXN]; 10 int main() 11 { 12 scanf("%s",a+1);scanf(" %s",b+1); 13 lena=strlen(a+1);lenb=strlen(b+1); 14 for(int i=1;i<=lena;++i) 15 for(int j=1;j<=lenb;++j) 16 { 17 if(a[i]==b[j])f[i][j]=(node){f[i-1][j-1].x+1,i-1,j-1}; 18 else 19 { 20 if(f[i-1][j].x>f[i][j-1].x)f[i][j]=(node){f[i-1][j].x,i-1,j}; 21 else f[i][j]=(node){f[i][j-1].x,i,j-1}; 22 } 23 } 24 printf("%d\n",f[lena][lenb].x); 25 return 0; 26 }
时间: 2024-10-27 04:36:07