zoukankan      html  css  js  c++  java
  • HDU 4545 魔法串 (简单DP)

    魔法串

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


    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
     
    Source
     
    Recommend
    liuyiding
     
     
     
     
     
    简单的水题
    不解释了
     
    //============================================================================
    // Name        : A.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <math.h>
    #include <string>
    #include <stdio.h>
    #include <math.h>
    using namespace std;
    const int MAXN=1010;
    char str1[MAXN],str2[MAXN];
    bool g[30][30];
    bool dp[MAXN][MAXN];
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int T;
        scanf("%d",&T);
        int iCase=0;
        while(T--)
        {
            iCase++;
            scanf("%s%s",&str1,&str2);
            memset(dp,false,sizeof(dp));
            memset(g,false,sizeof(g));
            int m;
            char s1[10],s2[10];
            scanf("%d",&m);
            while(m--)
            {
                scanf("%s%s",&s1,&s2);
                g[s1[0]-'a'][s2[0]-'a']=true;
            }
            int len1=strlen(str1);
            int len2=strlen(str2);
            dp[0][0]=true;
            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;
                        continue;
                    }
                    if(str1[i-1]==str2[j-1]&&dp[i-1][j-1])
                    {
                        dp[i][j]=true;
                        continue;
                    }
                    if(g[str2[j-1]-'a'][str1[i-1]-'a']&&dp[i-1][j-1])
                    {
                        dp[i][j]=true;
                        continue;
                    }
                }
            if(dp[len1][len2])printf("Case #%d: happy\n",iCase);
            else printf("Case #%d: unhappy\n",iCase);
    
        }
        return 0;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    547. Friend Circles
    399. Evaluate Division
    684. Redundant Connection
    327. Count of Range Sum
    LeetCode 130 被围绕的区域
    LeetCode 696 计数二进制子串
    LeetCode 116 填充每个节点的下一个右侧节点
    LeetCode 101 对称二叉树
    LeetCode 111 二叉树最小深度
    LeetCode 59 螺旋矩阵II
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3084407.html
Copyright © 2011-2022 走看看