POJ——2250 Compromise

来源:互联网 发布:淘宝怎么一键复制宝贝 编辑:程序博客网 时间:2024/06/06 02:52

就是最长公共子序列

#include <cstdio>#include <cstring>#include <iostream>using namespace std;// const int N=3500;char str1[105][35],str2[105][35];int main(){//freopen("text.txt","r",stdin);while(scanf("%s",str1[0])!=EOF&&str1[0][0]!='#'){int len1=1;while(scanf("%s",str1[len1])==1&&str1[len1][0]!='#')len1++;int len2=0;while(scanf("%s",str2[len2])==1&&str2[len2][0]!='#')len2++;int c[105][105];for(int i=1;i<=len1;i++)c[i][len2]=0;for(int i=1;i<=len2;i++)c[len1][i]=0;int i,j;for(i=len1-1;i>=0;i--)for(j=len2-1;j>=0;j--)if(strcmp(str1[i],str2[j])==0){c[i][j]=c[i+1][j+1]+1;s[i][j]=1;}else {s[i][j]=0;if(c[i][j+1]>c[i+1][j])c[i][j]=c[i][j+1];else c[i][j]=c[i+1][j];}int t=0,w=0;for(i=0;i<len1;i++)for(j=0;j<len2;j++)if(s[i][j]==1&&c[i][j]==c[0][0]-w){printf("%s ",str1[i]);w++;t=j+1;break;}putchar(10);}return 0;} 

0 0
原创粉丝点击