题目:https://acm.hdu.edu.cn/showproblem.php?pid=6170
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
写这个题要了一个多小时,,,还是太弱了,用的动态规划的思想,代码里面有注释
AC代码:
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
const int N = 2600;
int dp[N][N];
int main(){
ios_base::sync_with_stdio(false);
int t; cin >> t;
while(t--){
string a,b;
cin >> a;
cin >> b;
memset(dp,0,sizeof(dp)); //重置
a = ' ' + a;
b = ' ' + b;
dp[0][0] = 1; //两个空串是匹配的
dp[0][2] = 1; //a为空串,b为x*这样的形式也匹配的,长度大于两个类似aa*不匹配
for(int i = 1;i<a.size();i++){
for(int j = 1;j<b.size();j++){
if(a[i] == b[j] || b[j] == '.'){ //相等的时候或者bj是.的时候,看前一个的匹配情况
dp[i][j] = dp[i-1][j-1];
}
if(b[j] == '*'){ //两种情况,第一种是aab,aab*,第二种是aab, aabb*
dp[i][j] = dp[i][j-1] || dp[i][j-2];
}
if(a[i] == a[i-1] && b[j]=='*'){ //两种情况,第一种aabb,aab*, 第二种abbb,ab* ,为什么要加后面b[j]=='*'呢,因为要防止abbb,abx这样后面那个不是星号的情况
dp[i][j] = dp[i-1][j]||dp[i-1][j-1];
}
// cout << dp[i][j] << " \n"[j+1 == b.size()];
}
}
if(dp[a.size()-1][b.size()-1]){
cout << "yes" << endl;
}else{
cout << "no" << endl;
}
}
return 0;
}```