1. 程式人生 > >2017多校第9場 HDU 6170 Two strings DP

2017多校第9場 HDU 6170 Two strings DP

ems str php hdu 鏈接 兩種 namespace turn bit

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6170

題意:給了2個字符串,其中第2個字符串包含.和*兩種特別字符,問第二個字符串能否和第一個匹配。

解法:dp[i][j]代表在第一個串的i位置,第2個串的j位置是否可以匹配,然後按照‘*‘這個特殊情況討論轉移即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
bool dp[maxn][maxn];
char s1[maxn],s2[maxn];
bool match(char c1, char c2){
    if(c2==‘.‘) return 1;
    if(c1==c2) return 1;
    return 0;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(dp, 0, sizeof(dp));
        scanf("%s %s", s1+1,s2+1);
        int len1 = strlen(s1+1);
        int len2 = strlen(s2+1);
        dp[0][0]=1;
        for(int i=0; i<=len1; i++){
            for(int j=1; j<=len2; j++){
                if(i>=1){
                    if(match(s1[i],s2[j])) dp[i][j]|=dp[i-1][j-1];
                }
                if(s2[j]==‘*‘){
                    if(j>=2) dp[i][j]|=dp[i][j-2];
                    if(i){
                        int c=s2[j-1];
                        if(match(s1[i],c)){
                            dp[i][j]|=dp[i][j-1];
                            if(s1[i]==s1[i-1]){
                                dp[i][j]|=dp[i-1][j];
                            }
                        }
                    }
                }
            }
        }
        if(dp[len1][len2]) puts("yes");
        else puts("no");
    }
    return 0;
}

2017多校第9場 HDU 6170 Two strings DP