| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
真正自己过的DP,第一道,纪念~#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#define INF 100000
using namespace std;
int main()
{
char str[32];
string s1[105],s2[105],ans[105][105],s;
int i,j,len1,len2,f[105][105];
while(scanf("%s",str)!=EOF)
{
i=0;
while(str[0]!='#')
{
i++;
s1[i]=str;
scanf("%s",str);
}
len1=i;
i=0;
scanf("%s",str);
while(str[0]!='#')
{
i++;
s2[i]=str;
scanf("%s",str);
}
len2=i;
memset(f,0,sizeof(f));
f[0][0]=0;
ans[0][0]="";
for(i=1;i<=len1;i++)
{
f[i][0]=0;
ans[i][0]="";
}
for(i=1;i<=len2;i++)
{
f[0][i]=0;
ans[0][i]="";
}
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(s1[i]==s2[j])
{
if(f[i-1][j-1]+1>f[i][j])
{
f[i][j]=f[i-1][j-1]+1;
ans[i][j]=ans[i-1][j-1]+s1[i]+" ";
}
}
if(f[i][j-1]>f[i][j])
{
f[i][j]=f[i][j-1];
ans[i][j]=ans[i][j-1];
}
if(f[i-1][j]>f[i][j])
{
f[i][j]=f[i-1][j];
ans[i][j]=ans[i-1][j];
}
}
}
int len=ans[len1][len2].size();
s=ans[len1][len2];
cout<<s.erase(len-1,1)<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator