HDU 6170:Two strings

来源:互联网 发布:java 字符串补位 编辑:程序博客网 时间:2024/05/17 02:59

Two strings

                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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。用d[i][j]=1表示b中前i个字符与a中前j个字符匹配。


#include<bits/stdc++.h>using namespace std;char a[3000],b[3000];int d[3000][3000];int na,nb;int main(){    int T;cin>>T;    while(T--)    {        scanf("%s%s",a+1,b+1);        na=strlen(a+1);        nb=strlen(b+1);        memset(d,0,sizeof d);        d[0][0]=1;        for(int i=1;i<=nb;i++)        {            if(i==2&&b[i]=='*')d[i][0]=1;            for(int j=1;j<=na;j++)            {                if(b[i]=='.')d[i][j]=d[i-1][j-1];                else if(b[i]==a[j])d[i][j]=d[i-1][j-1];                else if(b[i]=='*')                {                    d[i][j]=d[i-2][j]|d[i-1][j];                    if(a[j-1]==a[j])d[i][j]=d[i][j]|d[i-1][j-1]|d[i][j-1];                }            }        }        puts(d[nb][na]?"yes":"no");    }    return 0;}



原创粉丝点击