HDU 4545 魔法串

     题目描述就不再赘述,意思是将一个串2进行两种操作(1)删除一个元素,(2)更改一个元素,但是是有条件的更改,要满足转换表,不能随意改换。最后问是否能将串2变为串1(保证len1<len2)。

     不知道还记不记得有这么一个题,要求将一个串变为另一个串,可以进行操作:任意删除、增加、改变字符,嗯。。貌似是叫什么edit distance的题,那么这题其实就是edit distance的晋级版,要求更苛刻了而已,思想还是DP,记录dp[i][j]代表将串str2前j个字符变为串str1前i个字符是否可行。

   

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define maxn 1006
using namespace std;

char str1[maxn];
char str2[maxn];
bool dp[maxn][maxn];
bool map[30][30];//制作26个字母表的对应转换 

int main()
{
    int Tcas,n;
    scanf("%d",&Tcas);
    for(int cas=1;cas<=Tcas;cas++)
    {
        scanf("%s%s",str1,str2);
        scanf("%d",&n);
        memset(dp,false,sizeof(dp));
        memset(map,false,sizeof(map));
        char s1[2],s2[2];
        
        while(n--)
        {
            scanf("%s%s",s1,s2);
            map[s1[0]-'a'][s2[0]-'a'] = true;
        }
        
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        
        for(int i=0;i<=len2;i++) dp[0][i] = true;
        
        for(int i=1;i<=len1;i++)
        {
            for(int j=1;j<=len2;j++)
            {
                if(dp[i][j-1]){
                    dp[i][j] = true;
                }else if(str1[i-1]==str2[j-1] && dp[i-1][j-1]){
                    dp[i][j] = true;
                }else if(dp[i-1][j-1] && map[str2[j-1]-'a'][str1[i-1]-'a']){
                    dp[i][j] = true;
                }
            }
        }
        
        printf("Case #%d: ",cas);  
        if(dp[len1][len2]) puts("happy");
        else puts("unhappy");
    }
    
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值