魔法串

魔法串

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2721    Accepted Submission(s): 981


Problem Description
  小明和他的好朋友小西在玩一个新的游戏,由小西给出一个由小写字母构成的字符串,小明给出另一个比小西更长的字符串,也由小写字母组成,如果能通过魔法转换使小明的串和小西的变成同一个,那么他们两个人都会很开心。这里魔法指的是小明的串可以任意删掉某个字符,或者把某些字符对照字符变化表变化。如:
    小西的串是 abba;
    小明的串是 addba;
    字符变化表 d b (表示d能转换成b)。
  那么小明可以通过删掉第一个d,然后将第二个d转换成b将串变成abba。

  现在请你帮忙判断:他们能不能通过魔法转换使两个人的串变成一样呢?
 

Input
  首先输入T,表示总共有T组测试数据(T <= 40)。
  接下来共T组数据,每组数据第一行输入小西的字符串,第二行输入小明的字符串(数据保证字符串长度不超过1000,小明的串的长度大于等于小西的,且所有字符均为小写字母)。接着输入字母表,先输入m,表示有m个字符变换方式(m< = 100),接着m行每行输入两个小写字母,表示前一个可以变为后一个(但并不代表后一个能变成前一个)。
 

Output
  对于每组数据,先输出Case数。
  如果可以通过魔法转换使两个人的串变成一样,输出“happy”,
  否则输出“unhappy”。
  每组数据占一行,具体输出格式参见样例。
 

Sample Input
  
  
2 abba addba 1 d b a dd 0
 

Sample Output
  
  
Case #1: happy Case #2: unhappy



解题报告:注意可以删掉多个字符。这道题是最长公共子序列的变式,当然也可以直接暴力解。

code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=1005;
char s1[maxn],s2[maxn];
int dp[30][30];

int main()
{
  //  freopen("input.txt","r",stdin);
    int t,m,k=1;
    scanf("%d",&t);
    while(t--){
        scanf("%s%s",s1,s2);
        memset(dp,0,sizeof(dp));
        scanf("%d",&m);
        char c,d;
        for(int i=0;i<m;i++){
            getchar();
            scanf("%c %c",&c,&d);
            dp[(c-'a')][d-'a']=1;
        }
        int r=0,flag=1;
        for(int i=0;i<strlen(s1);i++){
            int mark=0;
            for(int j=r;j<strlen(s2);j++){
                if(s2[j]==s1[i]||dp[(s2[j]-'a')][(s1[i]-'a')]){
                    r=j+1;
                    mark=1;
                    break;
                }
            }
            if(!mark){
                flag=0;
                break;
            }
        }
        if(flag) printf("Case #%d: happy\n",k++);
        else printf("Case #%d: unhappy\n",k++);
    }
}


最长公共子序列:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=1005;
char x[maxn],y[maxn];
int c[maxn][maxn];
int dp[30][30];

void lcsTable(){/*建立一张路径长度表*/
    int m=strlen(x)+1;
    int n=strlen(y)+1;
    for(int i=0;i<m;i++)
        c[i][0]=0;
    for(int j=0;j<n;j++)
        c[0][j]=0;
    for(int i=1;i<m;i++){
        for(int j=1;j<n;j++){
            if(x[i-1]==y[j-1] || dp[(y[j-1]-'a')][(x[i-1]-'a')]){
                c[i][j]=c[i-1][j-1]+1;
            }else if(c[i-1][j]>c[i][j-1]){
                c[i][j]=c[i-1][j];
            }else{
                c[i][j]=c[i][j-1];
            }
        }
    }
}

int main()
{
  //  freopen("input.txt","r",stdin);
    int t,m,k=1;
    scanf("%d",&t);
    while(t--){
        scanf("%s%s",x,y);
        memset(dp,0,sizeof(dp));
        scanf("%d",&m);
        char b,d;
        for(int i=0;i<m;i++){
            getchar();
            scanf("%c %c",&b,&d);
            dp[(b-'a')][d-'a']=1;
        }
        lcsTable();
        int m=strlen(x),n=strlen(y);
        if(c[m][n]==m)
            printf("Case #%d: happy\n",k++);
        else printf("Case #%d: unhappy\n",k++);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值