首页 > 其他 > 详细

2017多校第9场 HDU 6170 Two strings DP

时间:2017-08-22 20:08:04      阅读:377      评论:0      收藏:0      [点我收藏+]

题目链接: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

原文:http://www.cnblogs.com/spfa/p/7413411.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!