本文共 1373 字,大约阅读时间需要 4 分钟。
#include#include #include #include using namespace std;const int MAX = 120;char dp[MAX][MAX*1000][MAX];char str1[MAX*1000],str2[MAX];int aa[MAX],bb[MAX],cc[MAX];void Add(char *c,char *a,char *b){ int len1 = strlen(a); int len2 = strlen(b); memset(aa,0,sizeof(aa)); memset(bb,0,sizeof(bb)); memset(cc,0,sizeof(cc)); int len = max(len1,len2) + 3; for(int i = 0;i < len1;i++) aa[i] = a[len1-i-1]-'0'; for(int i = 0;i < len2;i++) bb[i] = b[len2-i-1]-'0'; int ct = 0; for(int i = 0;i < len;i++) { int s = aa[i] + bb[i] + ct; cc[i] = s%10; ct = s / 10; } int j; for(j = len;j>=0;j--) if(cc[j]) break; if(j < 0) c[0] = '0'; else for(int i = j;i>=0;i--) c[j-i] = cc[i] + '0'; c[j+1] = '\0';}int main(){ int n; scanf("%d",&n); while(n--) { scanf("%s%s",str1,str2); int len1 = strlen(str1); int len2 = strlen(str2); for(int i = 0; i <= len1; i++) strcpy(dp[0][i],"1"); for(int i = 1; i <= len2; i++) { strcpy(dp[i][0],"0"); for(int j = i; j<=len1; j++) { if(str2[i-1]==str1[j-1]) Add(dp[i][j],dp[i][j-1],dp[i-1][j-1]); else strcpy(dp[i][j],dp[i][j-1]); } } cout< <
转载地址:http://qbsgi.baihongyu.com/