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;
    }
    
  • 相关阅读:
    卷积神经网络
    降噪自动编码器(Denoising Autoencoder)
    栈式自动编码器(Stacked AutoEncoder)
    限制Boltzmann机(Restricted Boltzmann Machine)
    MATLAB conv2卷积的实现
    二维卷积的基本原理
    范数的物理意义(转)
    卷积的本质及物理意义(全面理解卷积)
    从稀疏表示到低秩表示(五)
    Android官方技术文档翻译——Ant 任务
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7416484.html
Copyright © 2011-2022 走看看