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 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 ssand 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
Copy
6 10 code*s codeforces
output
Copy
YES
input
Copy
6 5 vk*cup vkcup
output
Copy
YES
input
Copy
1 1 v k
output
Copy
NO
input
Copy
9 6 gfgf*gfgf gfgfgf
output
Copy
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".
题意:输入两个字符串,字符串里面可能有一个通配符,可以匹配任意小写英文字母,也可以匹配空字符,现在判断能不能把第一个字符串,改成第二个字符串。能YES,否则No。
题解:模拟 先判断第一个字符是否有*,没有的话,判断a==b么,然后Yes或者NO,另外一种含*,python从*的位置分割成两个字符串,判断第二个字符串是否有前面字符串,c++里面记录前后相同字符的下标,直到一个字母不同。
c++:查找函数传送门:点击传送门
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,l=0,r=0;
string a,b,s1,s2;
cin>>n>>m>>a>>b;
if(a.find('*')==a.npos)///查找函数,判断a中没有*,返回npos
a==b?puts("YES"):puts("NO");
else
{
while(l<n&&l<m&&a[l]==b[l])
l++;
while(r+l<n&&r+l<=m&&a[n-r]==b[m-r])
r++;
if(r+l==n&&a[l]=='*') puts("YES");
else puts("NO");
}
return 0;
}
python:s.startswith(),s.endswith匹配s字符串中前后是否有该字符串
n,m=map(int,input().split())
a,b=input(),input()
if "*" not in a:
print("YES"if a==b else "NO")
else:
l,r=a.split('*')
if b.startswith(l) and b.endswith(r) and len(l)+len(r)<=len(b):
print("YES")
else: print("NO")