题目地址:http://jobdu.sinaapp.com/problem.php?cid=1040&pid=97
C语言源码:
#include<stdio.h>#include<string.h>int dp[110][110];char s1[110],s2[110];int len1,len2;int max(int a,int b){ return a>b?a:b;}int compu(int i,int j){ int a,b; if(i==0||j==0) return 0; else { if(s1[i-1]==s2[j-1]) { if(dp[i-1][j-1]!=-1) return dp[i-1][j-1]+1; else return compu(i-1,j-1)+1; } else { if(dp[i-1][j]!=-1) a=dp[i-1][j]; else a=compu(i-1,j); if(dp[i][j-1]!=-1) b=dp[i][j-1]; else b=compu(i,j-1); return max(a,b); } }}int main(){ int i,j; while(scanf("%s %s",s1,s2)!=EOF) { len1=(int)strlen(s1); len2=(int)strlen(s2); for(i=0;i<=len1;i++) for(j=0;j<=len2;j++) dp[i][j]=-1; for(i=0;i<=len1;i++) for(j=0;j<=len2;j++) dp[i][j]=compu(i,j); printf("%d\n",dp[len1][len2]); }}