hdu 6170 dp 2017 Multi-University Training Contest

来源:互联网 发布:建筑工程预算软件 编辑:程序博客网 时间:2024/05/29 07:15

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
3aaa*abba.*abbaab
 

Sample Output
yesyesno
 

Source
2017 Multi-University Training Contest - Team 9


好久没有做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 2505int 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;}


原创粉丝点击