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;
    }
    
  • 相关阅读:
    C#的委托事件总结
    iOS的录屏功能
    Unity通过NTP获取网络时间
    Unity的弱联网Json数据传输
    Unity场景和代码合并以及UnityYAMLMerge的使用
    Unity学习--捕鱼达人笔记
    Yomob广告在cocos2dx安卓平台的Demo
    谷歌广告Admob在cocos2dx上通过回调实现底部Banner
    JZ2440 裸机驱动 第13章 LCD控制器(2)
    JZ2440 裸机驱动 第13章 LCD控制器(1)
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7416484.html
Copyright © 2011-2022 走看看