codeforces 56D Changing a String 区间dp,字符串

来源:互联网 发布:文本框空js弹出遮罩层 编辑:程序博客网 时间:2024/06/07 01:09

点击打开链接

是一个字符串变为目标字符串,可以执行插入,置换和删除3种操作,求最少操作数。

很神奇的dp,dp[i][j]表示原字符串的前i位变为目标串的前j位所需最小步数。

#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<cmath>#include<iomanip>#include<map>#include<algorithm>#include<queue>#include<set>#define inf 10000000#define pi acos(-1.0)#define eps 1e-8#define seed 131using namespace std;typedef pair<int,int> pii;typedef unsigned long long ULL;typedef long long LL;const int maxn=1005;int dp[maxn][maxn];char s1[maxn];char s2[maxn];int n,m;void dfs(int a,int b){    if(a==0&&b==0)        return;    if(a-1>=0&&dp[a][b]==dp[a-1][b]+1){        dfs(a-1,b);        printf("DELETE %d\n",b+1);    }    else if(b-1>=0&&dp[a][b]==dp[a][b-1]+1){        dfs(a,b-1);        printf("INSERT %d %c\n",b,s2[b]);    }    else{        dfs(a-1,b-1);        if(s1[a]!=s2[b])            printf("REPLACE %d %c\n",b,s2[b]);    }}int main(){    scanf("%s",s1+1);    scanf("%s",s2+1);    n=strlen(s1+1);    m=strlen(s2+1);    dp[0][0]=0;    for(int i=1;i<=n;i++)        dp[i][0]=i;    for(int i=1;i<=m;i++)        dp[0][i]=i;    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        dp[i][j]=min(min(dp[i-1][j],dp[i][j-1])+1,dp[i-1][j-1]+(s1[i]!=s2[j]));    printf("%d\n",dp[n][m]);    dfs(n,m);    return 0;}


0 0
原创粉丝点击