hdu 6170 dp 2017 Multi-University Training Contest - Team 9

Two strings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 674    Accepted Submission(s): 267


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
 

Source


好久没有做dp了,不敏感了,接下来多做做dp吧


转载于某大牛

题意:
给你两个字符串:第一个字符串只包含小写大写字母
第二个字符串除了字母之外,还有'.'和'*',其中'.'可以当成任意一个字符,'*'表示前面那个字符可以重复若干次
当然也可以重复0次,例如a.*A可以是aaaA,可以是abbbbA等等,也可以是aA
问两个串能不能匹配(即第二个字符串某种情况下和第一个字符串一模一样)

题解:
因为长度只有2500,很好想到DP,dp[x][y]表示第二个字符串长度为x,第一个字符串长度为y能否完美匹配(1or0)


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 2505
int dp[MAXN][MAXN];
char str1[MAXN],str2[MAXN];

int main()
{
    int T;
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",str1+1);
        scanf("%s",str2+1);
        int len1=strlen(str1+1);
        int len2=strlen(str2+1);

        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=len2;i++){
            if(str2[i]=='*'&&i==2)
                dp[i][0]=1;
            for(int j=1;j<=len1;j++){
                if(str2[i]=='.')
                    dp[i][j]=dp[i-1][j-1];
                else if(str2[i]!='*'){
                    if(str2[i]==str1[j])
                        dp[i][j]=dp[i-1][j-1];
                }
                else{
                    dp[i][j]=max(dp[i-2][j],dp[i-1][j]);
                    if((dp[i-1][j-1]==1||dp[i][j-1]==1)&&str1[j]==str1[j-1])
                        dp[i][j] = max(dp[i][j],max(dp[i][j-1],dp[i-1][j-1]));
                }
            }
        }
        if(dp[len2][len1]) puts("yes");
        else               puts("no");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值