第 2 測資點(20%): WA (line:1)
您的答案為: 22 正確答案為: 23第 3 測資點(20%): AC (0ms, 392KB)
第 4 測資點(20%): AC (0ms, 384KB)
第 5 測資點(20%): AC (0ms, 384KB)
我也這樣...
#include<stdio.h> #include<string.h> char ans[101]; short lcs(char s1[],char s2[]){ short dp[101][101] = {0},route[101][101] = {0},i,j,l1 = strlen(s1),l2 = strlen(s2); for(i=1;i<=l1;i++){ for(j=1;j<=l2;j++){ if(s1[i-1]==s2[j-1]){ dp[i][j] = dp[i-1][j-1]+1; route[i][j] = 1; } else{ if(dp[i-1][j]>dp[i][j-1]){ dp[i][j] = dp[i-1][j]; route[i][j] = 2; } else{ dp[i][j] = dp[i][j-1]; route[i][j] = 3; } } } } int x = l1,y = l2,l3 = dp[l1][l2]; ans[l3--] = '\0'; while(x>0&&y>0){ switch(route[x][y]){ case 1: ans[l3--] = s1[x-1]; x--; y--; break; case 2: x--; break; case 3: y--; break; } } return dp[l1][l2]; } int main(){ char s1[101],s2[101],s3[101]; while(scanf("%s%s%s",s1,s2,s3)!=EOF){ short s1s2,anss3; s1s2 = lcs(s1,s2); strcpy(s1,ans); anss3 = lcs(s1,s3); printf("%hd\n",anss3); } }