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 |
lcs的变形啊状态转移方程: if(a[i-1]==b[j-1])f[i][j]=min(min(f[i-1][j-1],f[i-1][j]+1),f[i][j-1]+1); else f[i][j]=min(min(f[i-1][j-1]+1,f[i-1][j]+1),f[i][j-1]+1); 全部代码: #include<iostream> #include<cstdio> #include<string> #include<cstring> int m,n,f[1001][1001]; using namespace std; int main() { string a,b; while(scanf("%d",&m)!=EOF) { cin>>a; scanf("%d",&n); cin>>b; for(int i=0;i<=m;i++)f[i][0]=i; for(int j=0;j<=n;j++)f[0][j]=j; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(a[i-1]==b[j-1])f[i][j]=min(min(f[i-1][j-1],f[i-1][j]+1),f[i][j-1]+1); else f[i][j]=min(min(f[i-1][j-1]+1,f[i-1][j]+1),f[i][j-1]+1); } } cout<<f[m][n]<<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