Two strings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1615 Accepted Submission(s): 639
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 “*”.
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).
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
跟leetcode一个题很像,只不过这个题的"."只能匹配一种字符,所以特判的时候要判断是否跟上一个字符相等
具体的状态转移写在注释里了
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <sstream>
#include <stdio.h>
using namespace std;
bool isMatch(const string& s, const string& p) {
int slen = s.size();
int plen = p.size();
//dp[i + 1][j + 1]表示 s[i]跟p[j]当前的匹配情况
/**
* bp[i + 1][j + 1]: if s[0..i] matches p[0..j]
* if p[j] != '*'
* bp[i + 1][j + 1] = bp[i][j] && s[i] == p[j]
* if p[j] == '*', denote p[j - 1] with x,
* then bp[i + 1][j + 1] is true iff any of the following is true
* 1) "x*" repeats 0 time and matches empty: bp[i + 1][j -1]
* 2) "x*" repeats 1 time and matches x: bp[i + 1][j]
* 3) "x*" repeats >= 2 times and matches "x*x": s[i] == x && bp[i][j + 1]
* '.' matches any single character
可以画一个方格图,就像最长公共子序列一样,就比较好理解了
*/
bool dp[slen + 1][plen + 1];
dp[0][0] = 1;
for (int i = 0; i < slen; ++i) {
dp[i + 1][0] = 0;
}
for (int i = 0; i < plen; ++i) {
dp[0][i + 1] = i > 0 && p[i] == '*' && dp[0][i - 1];
}
for (int i = 0; i < slen; ++i) {
for (int j = 0; j < plen; ++j) {
if (p[j] == '*') { //x*匹配0个x的时候 x*匹配1个x的时候 x*匹配>=2个x的时候 x*x
dp[i + 1][j + 1] = (j > 0 && dp[i + 1][j - 1]) || dp[i + 1][j] || (dp[i][j + 1] && j > 0 && ((p[j - 1] == '.' && s[i - 1] == s[i])|| s[i] == p[j - 1]));
} else {
dp[i + 1][j + 1] = dp[i][j] && (s[i] == p[j] || p[j] == '.');
}
}
}
return dp[slen][plen];
}
int main() {
//freopen("in.txt", "r", stdin);
int t;
cin >> t;
while (t--) {
string s1, s2;
cin >> s1 >> s2;
if (isMatch(s1, s2)) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
}
}