HDU 6170 Two strings

来源:互联网 发布:增值税发票认证软件 编辑:程序博客网 时间:2024/05/18 16:14

Two strings

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


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


题意:
有两个字符串,问你第二个字符串和第一个字符串能否匹配,第二个字符串有两种符号,
’.’可以匹配任意字符,’*’表示前一个字付可以重复零次或多次
注意“a*”是可以匹配空串的
题解:
其余题解有DP 和 C++库中自带的正则库 不做多余解释
注意从后往前 “.*.*”或“a*a*”可以当作一个情况处理即可
别把条件落下
(其实代码准确度不高 被队友HACK了)

#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<stack>#include<math.h>#define ll long longusing namespace std;int main(){    int t;    char str1[3000],str2[3000];    scanf("%d",&t);    while(t--){        scanf("%s %s",str1+1,str2+1);        int len1 = strlen(str1+1),len2 = strlen(str2+1);        for(int i = 3;i < len2;i++){            if(str2[i] == '*' && str2[i-2] == '*' && str2[i-1] == str2[i-3]){                for(int j = i-1;j < len2;j++)                    str2[j] = str2[j+2];                len2 -=2;            }        }        str1[0] = '@',str2[0] = '@';//        printf("%s %s\n",str1,str2);        int ans = 0;        while(len1 >= 0 && len2 >= 0){            if(!len1 && !len2){                ans = 1;                break;            }            if(str1[len1] == str2[len2]){                len1--,len2--;                continue;            }            if(str2[len2] == '.'){                len1--,len2--;                continue;            }            if(str2[len2] == '*'){                if(str2[len2-1] != str1[len1] && str2[len2-1] != '.'){                    len2 -= 2;                    continue;                }                if(str2[len2-1] == '.'){                    int tmp = len1;                    while(str1[len1] == str1[tmp])                        len1--;                    len2 -=2;                    continue;                }                    if(str2[len2-1] == str1[len1]){                    int tmp = len1;                    while(str1[len1] == str1[tmp])                        len1--;                    len2 -= 2;                    continue;                }            }                if(str2[len2] != str1[len1]){                if((str1[len1 + 1] == str2[len2]) && str2[len2 + 2] == '*'){                    int tmp = 0;                    while(str1[len1 + 1 + tmp] == str2[len2]){                        tmp++,len2--;                    }                    continue;                }                        break;            }                }        if(ans)            puts("yes");        else            puts("no");    }    return 0;}