A. Single Wildcard Pattern Matching
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two strings ss and tt. The string ss consists of lowercase Latin letters and at most one wildcard character '*', the string ttconsists only of lowercase Latin letters. The length of the string ss equals nn, the length of the string tt equals mm.
The wildcard character '*' in the string ss (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of ss can be replaced with anything. If it is possible to replace a wildcard character '*' in ss to obtain a string tt, then the string tt matches the pattern ss.
For example, if s=s="aba*aba" then the following strings match it "abaaba", "abacaba" and "abazzzaba", but the following strings do not match: "ababa", "abcaaba", "codeforces", "aba1aba", "aba?aba".
If the given string tt matches the given string ss, print "YES", otherwise print "NO".
Input
The first line contains two integers nn and mm (1≤n,m≤2⋅1051≤n,m≤2⋅105) — the length of the string ss and the length of the string tt, respectively.
The second line contains string ss of length nn, which consists of lowercase Latin letters and at most one wildcard character '*'.
The third line contains string tt of length mm, which consists only of lowercase Latin letters.
Output
Print "YES" (without quotes), if you can obtain the string tt from the string ss. Otherwise print "NO" (without quotes).
Examples
input
6 10
code*s
codeforces
output
YES
input
6 5
vk*cup
vkcup
output
YES
input
1 1
v
k
output
NO
input
9 6
gfgf*gfgf
gfgfgf
output
NO
Note
In the first example a wildcard character '*' can be replaced with a string "force". So the string ss after this replacement is "codeforces" and the answer is "YES".
In the second example a wildcard character '*' can be replaced with an empty string. So the string ss after this replacement is "vkcup" and the answer is "YES".
There is no wildcard character '*' in the third example and the strings "v" and "k" are different so the answer is "NO".
In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string tt so the answer is "NO".
***函数substr(begin,end);
我们把s串在*号左右两边拆开,t串与s串对应的位置也记录一下。然后对比。
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;
typedef long long ll;
const int maxn = 2e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;
int n, m;
string u, v;
int main(){
while(scanf("%d%d",&n,&m) != EOF){
cin >> u >> v;
int pos = -1;
int flag = 0;
for(int i = 0; i < n; i++){
if(u[i] == '*'){
pos = i;
break;
}
}
if(pos != -1){
if(n > m+1) flag = 0;
else{
string ul, ur, vl, vr;
ul = u.substr(0, pos);
ur = u.substr(pos + 1, n);
int len = ur.size();
vl = v.substr(0, pos);
vr = v.substr(m - len, m);
//cout << ul << " " << vl << endl;
//cout << ur << " " << vr << endl;
if(ul == vl && ur == vr) flag = 1;
else flag = 0;
}
}
else{
if(u == v) flag = 1;
else flag = 0;
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}