A. Single Wildcard Pattern Matching
You are given two strings ss and tt. The string ss consists of lowercase Latin letters and at most one wildcard character '*', the string tt consists 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
題意:給出長度爲n和m的兩個字符串s,x.其中s內含有“*”字符,星號可以代替任何內容(包括空串),x與s相比除去星號代表的內容後如果相等,則輸出YES。
substr()函數的介紹:https://blog.csdn.net/Xylon_/article/details/81532238
後臺數據簡直喪心病狂TAT
代碼:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define closeio std::ios::sync_with_stdio(false)
int main()
{
int n,m,i,len,flag=0;
string s,x,s1,s2;
cin>>n>>m;
cin>>s>>x;
for(i=0; i<n; i++)
if(s[i]=='*') //找到星號的位置
{
flag=1;
break;
}
if(n-1>m) //如果x串小於s串,必不可能相等
{
cout<<"NO"<<endl;
return 0;
}
if(flag)
{
s1=s.substr(0,i);
s2=s.substr(i+1,n-i-1);
//cout<<s1<<" "<<s2<<endl;
len=s2.length();
//cout<<x.substr(0,i)<<" "<<x.substr(m-len,m-1)<<endl;
if(x.substr(0,i)==s1&&x.substr(m-len,m-i)==s2)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else
{
if(s==x)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}