1 条题解
-
0
C++ :
#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; struct node { int x,y; }p[1001][1001]; char s1[1100], s2[1100],s[1100]; int f[1100][1100],len1,len2; int mymax ( int x, int y ){ return x > y ? x : y; } int main () { scanf ( "%s", s1 + 1 ); len1 = strlen ( s1 + 1 ); scanf ( "%s", s2 + 1 ); len2 = strlen ( s2 + 1 ); memset(f,0,sizeof(f)); for (int i = 1; i <= len1; i ++ ) { for (int j = 1; j <= len2; j ++ ) { if ( s1[i] == s2[j] ) { f[i][j] = f[i - 1][j - 1] + 1; p[i][j].x=i; p[i][j].y=j; } else { if( f[i-1][j] > f[i][j-1] ) { f[i][j]=f[i-1][j]; p[i][j]=p[i-1][j]; } else { f[i][j]=f[i][j-1]; p[i][j]=p[i][j-1]; } } } } printf ( "%d\n", f[len1][len2] ); int x=len1,y=len2,len=0; while( f[x][y]>0) { int xx,yy; xx=x;yy=y; s[++len]=s1[ p[xx][yy].x ];// s[++len]=s2[ p[xx][yy].y]; x=p[xx][yy].x-1; y=p[xx][yy].y-1; } for(int i=len;i>=1;i--) printf("%c",s[i]); printf("\n"); return 0; }
- 1
信息
- ID
- 1171
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者