zoj 1459 String Distance and ...

zoj 1459 String Distance and ...,第1张

zoj 1459 String Distance and ...
#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define  M 81#define Min(a,b) ((a)<(b)?(a):(b))char s[M],t[M];int n,m,dp[M][M],to[M][M];void outpath(){int p=0;while(n!=0 || m!=0 ){to[n][m]?p++:1;switch(to[n][m]){case 1 : cout<<p<<" Delete "<<n<<endl; break;case 2 : cout<<p<<" Insert "<<n+1<<","<<t[m]<<endl;break;case 3 : cout<<p<<" Replace "<<n<<","<<t[m]<<endl;break;}switch(to[n][m]){case 3 :case 0 : n--,m--;break;case 1 : n--; break;case 2 : m--; break;}}}int main(){while(~scanf("%s %s",s+1,t+1) ){int i,j;n=strlen(s+1);m=strlen(t+1);for(i=1;i<=n;i++){dp[i][0]=dp[i-1][0]+1;to[i][0]=1;}for(j=1;j<=m;j++){dp[0][j]=dp[0][j-1]+1;to[0][j]=2;}for(i=1;i<=n;i++){ for(j=1;j<=m;j++){if(s[i]==t[j]){dp[i][j]=dp[i-1][j-1];to[i][j]=0;}else{dp[i][j]=Min(Min(dp[i][j-1],dp[i-1][j-1]),dp[i-1][j])+1;if(dp[i][j]==dp[i-1][j]+1)to[i][j]=1;if(dp[i][j]==dp[i][j-1]+1)to[i][j]=2;if(dp[i][j]==dp[i-1][j-1]+1)to[i][j]=3;}}}cout<<dp[n][m]<<endl;outpath();}return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4910160.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存