zoukankan      html  css  js  c++  java
  • HDU 6170 Two String 动态规划

    题目链接

    2017多校训练9-1010


    Problem Description
    Giving two strings and you should judge if they are matched.
    The first string contains lowercase letters and uppercase letters.
    The second string contains lowercase letters, uppercase letters, and special symbols: “.” and “*”.
    . can match any letter, and * means the front character can appear any times. For example, “a.b” can match “acb” or “abb”, “a*” can match “a”, “aa” and even empty string. ( “*” will not appear in the front of the string, and there will not be two consecutive “*”.
    .
    
    Input
    The first line contains an integer T implying the number of test cases. (T≤15)
    For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
    
    
    Output
    For each test case, print “yes” if the two strings are matched, otherwise print “no”.
    
    
    Sample Input
    3
    aa
    a*
    abb
    a.*
    abb
    aab
    
    
    Sample Output
    yes
    yes
    no
    

    可以去找LeetCode Problem 10,题目极其类似

    本题.*不能匹配ab。


    思路

    考虑dp(i,j)表示s串的前i个字符能否匹配p串的前j个字符。

    状态转移方程:

    没有出现*
    dp[i][j] = dp[i-1][j-1] ,    s[i-1] == p[j-1]或者p[j-1]=='.'
    如果出现*
    则dp[i][j] = dp[i][j-2](0次缩放),dp[i][j-1](1次缩放),dp[i-1][j](多次缩放)
    其他情况
    dp[i][j] = 0
    

    边界值是一定匹配失败,还有匹配空串的情况

    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    #include <string>
    #include <math.h>
    #include <bitset>
    #include <ctype.h>
    using namespace std;
    typedef pair<int,int> P;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-9;
    const int N = 3000 + 5;
    const int mod = 1e9 + 7;
    int T;
    char s1[N],s2[N];
    int dp[N][N];
    bool ismatch(const char *s, const char *p)
    {
        int ls = strlen(s);
        int lp = strlen(p);
        for(int i = 0; i <= ls; i++)
            for(int j = 0; j <= lp; j++)
            dp[i][j] = 1;
        for(int i = 1; i <= ls; i++)
            dp[i][0] = 0;
        for(int j = 1; j <= lp; j++)
        {
            if(p[j-1] == '*') dp[0][j] = dp[0][j-2];
            else  dp[0][j] = 0;
        }
    
        for(int i = 1; i <= ls; i++)
        {
            for(int j = 1; j <= lp; j++)
            {
                if(p[j-1] == '.' || s[i-1] == p[j-1])
                    dp[i][j] = dp[i-1][j-1];
                else if(p[j-1] == '*')
                {
                    dp[i][j] = (j >= 2 && dp[i][j-2]) || (dp[i][j-1]) || (dp[i-1][j]&&(s[i-1] == p[j-2] || (p[j-2] == '.' && i >= 2 && s[i-1] == s[i-2])));
                }
                else
                    dp[i][j] = 0;
            }
        }
        return dp[ls][lp];
    }
    
    int main()
    {
        scanf("%d", &T);
        while(T--)
        {
            scanf("%s%s", s1, s2);
            printf("%s
    ", ismatch(s1,s2) ? "yes" : "no");
        }
        return 0;
    }
    
  • 相关阅读:
    JQuery中的bind()和unbind()的理解
    一行代码实现数组去重(ES6)
    vue.js学习笔记(Vuejs——组件——props数据传递)
    判断一个字符串书否存在某个字符,取出一段字符串括号中的内容
    在vue中使用高德地图vue-amap
    在egg.js中使用mongodb
    使用Element的upload上传组件,不使用action属性上传
    下载和安装mongodb4.2.0+robmongo可视化工具
    数组对象根据某个属性取出重复的个数
    钉钉小程序----使用阿里的F2图表
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7416484.html
Copyright © 2011-2022 走看看