• HDOJ 1501 Zipper 【简单DP】


    HDOJ 1501 Zipper 【简单DP】

    Problem Description
    Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

    For example, consider forming “tcraete” from “cat” and “tree”:

    String A: cat
    String B: tree
    String C: tcraete

    As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming “catrtee” from “cat” and “tree”:

    String A: cat
    String B: tree
    String C: catrtee

    Finally, notice that it is impossible to form “cttaree” from “cat” and “tree”.

    Input
    The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

    For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.

    Output
    For each data set, print:

    Data set n: yes

    if the third string can be formed from the first two, or

    Data set n: no

    if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.

    Sample Input

    3
    cat tree tcraete
    cat tree catrtee
    cat tree cttaree

    Sample Output

    Data set 1: yes
    Data set 2: yes
    Data set 3: no

    题意
    给出三串字符串 a, b, c 判断 字符串C 中的每个字符 是不是分别从a, b 字符串中 顺序取出的。也就是说 从c字符串中 按顺序 能不能取出 a ,b 字符串 可以不连续 但顺序不能变

    思路
    我们可以从最后一个字符来判断 ,若要满足题目要求,字符串c的最后一个字符肯定是字符串a或者字符串b中的最后一个 如果满足 就往前推 如果不满足 就break 掉

    所以 其实是一个动态规划的过程 我们从前往后看 也是一样的

    我们用dp[i][j]来标记,i 代表取a串的前i个字符, j 代表取 b串的前j 个字符
    我们可以发现 dp[i][j] 的状态 其实是由 dp[i - 1][j] || dp[i][j - 1] 转移过来的

    如果 dp[i - 1][j] 的状态是 1 的话 那么 只需要满足 a[i - 1] == c[i + j - 1] 为什么要减1 是因为 字符串从0开始计数
    如果满足 那么 dp[i][j] 的状态也是1

    同理 或者满足 dp[i][j - 1] 的状态是1 并且满足 b[j - 1] == c[i + j - 1] 那么 dp[i][j] 的状态是1

    上面两种条件 只需要满足一个 就可以转移过来了
    否则 dp[i][j] 的状态就是0

    AC代码

    #include <bits/stdc++.h>   //简单DP 
    using namespace std;
    const int maxn = 2 * 1e2 + 5;
    int dp[maxn][maxn];
    int main()
    {
        int t;
        cin >> t;
        int i, j, k;
        for (k = 1; k <= t; k++)
        {
            string s[3];
            int len[3];
            for (i = 0; i < 3; i++)
            {
                cin >> s[i];
                len[i] = s[i].size();
            }   
            memset(dp, 0, sizeof(dp));
            for (i = 0; i < len[0]; i++)
            {
                if (s[0][i] == s[2][i])
                    dp[i + 1][0] = 1;
                else                      //如果不BREAK 存在BUG  eg:  ca t dat  输出 yes 
                    break;
            }
            for (i = 0; i < len[1]; i++)
            {
                if (s[1][i] == s[2][i])
                    dp[0][i + 1] = 1;
                else
                    break;
            }
            for (i = 1; i <= len[0]; i++)
            {
                for (j = 1; j <= len[1]; j++)
                {
                    if (dp[i - 1][j] && s[0][i - 1] == s[2][i + j - 1])
                        dp[i][j] = 1;
                    if (dp[i][j - 1] && s[1][j - 1] == s[2][i + j - 1])
                        dp[i][j] = 1;
                }
            }
            printf("Data set %d: ", k);
            if (dp[len[0]][len[1]])
                cout << "yes
    ";
            else
                cout << "no
    ";
        }
    }
  • 相关阅读:
    当select框变化时 获取select框中被选中的值
    表字段添加、删除、修改
    初步了解JSONP
    通过 Chrome Workspace 调试本地项目(修改样式时及时保存)
    XAMPP PHP redis 扩展
    在右键菜单中加入 用...打开
    PHP unset 后恢复数组索引
    LINUX VI 常用命令
    排行榜 文字向上滚动效果
    jq 获取除节假日与周六日 外的日期 和 星期
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433399.html
Copyright © 2020-2023  润新知