CF1023A Single Wildcard Pattern Matching 题解

题目

链接

https://www.luogu.com.cn/problem/CF1023A

字面描述

题面翻译

题目描述

现在你得到了两个字符串 s , t s,t s,t,其中 s s s字符串包含小写英文字母以及不多于一个的 ∗ * 号,而 t t t字符串只包含小写英文字母。 s s s字符串的长度为 n n n,而 t t t字符串的长度为 m m m

s s s字符串中的 ∗ * 号可以被替换为任意一个仅含有小写字母字符串(当然也可以是空字符串),字符串中的其他字符不能被更改或者调换顺序。如果将 ∗ * 号替换为某个任意字符串之后, s s s字符串可以变成 t t t字符串,那么我们称 s , t s,t s,t两个字符串是匹配的。

例如,字符串 s = a b a ∗ a b a s=aba*aba s=abaaba与字符串 a b a a b a , a b a c a b a , a b a z z z a b a abaaba,abacaba,abazzzaba abaaba,abacaba,abazzzaba都是匹配的,但与字符串 a b c a a b a , a b a b a , l u o g u , a b a ? a b a , a b a 1 a b a abcaaba,ababa,luogu,aba?aba,aba1aba abcaaba,ababa,luogu,aba?aba,aba1aba都是不匹配的。

如果我们给出的字符串 s , t s,t s,t是匹配的,输出 Y E S YES YES,否则输出 N O NO NO

输入输出格式

输入格式

第一行是两个整数$n,m\left( 1\leqslant n,m\leqslant 2\cdot 10^5 \right) ,分别表示字符串 ,分别表示字符串 ,分别表示字符串s 和字符串 和字符串 和字符串t$的长度。

第二行包含一个长度为 n n n的字符串 s s s,保证字符串中只含有小写字母和不多于一个的 ∗ *

第三行包含一个长度为 m m m的字符串 t t t,保证字符串中只含有小写字母。

输出格式

如果你可以通过字符串 s s s得到字符串 t t t,也就是说二者是匹配的,那么输出 Y E S YES YES,否则输出 N O NO NO




源码区:

## 题目描述
现在你得到了两个字符串$s,t$,其中$s$字符串包含小写英文字母以及不多于一个的$*$号,而$t$字符串只包含小写英文字母。$s$字符串的长度为$n$,而$t$字符串的长度为$m$。

在$s$字符串中的$*$号可以被替换为任意一个仅含有小写字母字符串(当然也可以是空字符串),字符串中的其他字符不能被更改或者调换顺序。如果将$*$号替换为某个任意字符串之后,$s$字符串可以变成$t$字符串,那么我们称$s,t$两个字符串是匹配的。

例如,字符串$s=aba*aba$与字符串$abaaba,abacaba,abazzzaba$都是匹配的,但与字符串$abcaaba,ababa,luogu,aba?aba,aba1aba$都是不匹配的。

如果我们给出的字符串$s,t$是匹配的,输出$YES$,否则输出$NO$。

## 输入输出格式
### 输入格式
第一行是两个整数$n,m\left( 1\leqslant n,m\leqslant 2\cdot 10^5 \right) $,分别表示字符串$s$和字符串$t$的长度。

第二行包含一个长度为$n$的字符串$s$,保证字符串中只含有小写字母和不多于一个的$*$。

第三行包含一个长度为$m$的字符串$t$,保证字符串中只含有小写字母。
### 输出格式
如果你可以通过字符串$s$得到字符串$t$,也就是说二者是匹配的,那么输出$YES$,否则输出$NO$。

题目描述

You are given two strings $ s $ and $ t $ . The string $ s $ consists of lowercase Latin letters and at most one wildcard character ‘*’, the string $ t $ consists only of lowercase Latin letters. The length of the string $ s $ equals $ n $ , the length of the string $ t $ equals $ m $ .

The wildcard character ‘*’ in the string $ s $ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $ s $ can be replaced with anything. If it is possible to replace a wildcard character ‘*’ in $ s $ to obtain a string $ t $ , then the string $ t $ matches the pattern $ s $ .

For example, if $ 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 $ t $ matches the given string $ s $ , print “YES”, otherwise print “NO”.

输入格式

The first line contains two integers $ n $ and $ m $ ( $ 1 \le n, m \le 2 \cdot 10^5 $ ) — the length of the string $ s $ and the length of the string $ t $ , respectively.

The second line contains string $ s $ of length $ n $ , which consists of lowercase Latin letters and at most one wildcard character ‘*’.

The third line contains string $ t $ of length $ m $ , which consists only of lowercase Latin letters.

输出格式

Print “YES” (without quotes), if you can obtain the string $ t $ from the string $ s $ . Otherwise print “NO” (without quotes).

样例 #1

样例输入 #1
6 10
code*s
codeforces
样例输出 #1
YES

样例 #2

样例输入 #2
6 5
vk*cup
vkcup
样例输出 #2
YES

样例 #3

样例输入 #3
1 1
v
k
样例输出 #3
NO

样例 #4

样例输入 #4
9 6
gfgf*gfgf
gfgfgf
样例输出 #4
NO

提示

In the first example a wildcard character ‘*’ can be replaced with a string “force”. So the string $ s $ 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 $ s $ 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 $ t $ so the answer is “NO”.

思路

此题的重大变量就是有没有“ * ”号

如果没有“ * ”号,就是两个字符串一一比较

如果有 “ * ”号,就是将 " * " 号前的a字符串和b对应的字符串比较," * "后则相反。

代码实现

#include<bits/stdc++.h>
using namespace std;

const int maxn=2e5+10;
int n,m,op;
char a[maxn],b[maxn];
bool vis[maxn];
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf(" %c",&a[i]);
		if(a[i]=='*')op=i;// op 记录 *号位置 
	}	
	for(int i=1;i<=m;i++)scanf(" %c",&b[i]);
	//printf("%d ",op);
	//没有*号只是字符串比较
	if(op==0){
		bool flag=true;
		if(n==m){
			for(int i=1;i<=n;i++){
				if(a[i]!=b[i]){
					flag=false;
					break;
				}
			}
		} 
		else flag=false;
		if(flag)printf("YES\n");
		else printf("NO\n");
		return 0;
	}
	//按位比较
	bool flag=true;
	if(n-1>m)flag=false;
	else {
		for(int i=1;i<op;i++){
			if(a[i]!=b[i]){
				flag=false;
				break;
			}
		}
		for(int i=n;i>op;i--){
			if(a[i]!=b[m-n+i]){
				flag=false;
				//printf("1\n");
				break;
			}
		}
	}
	if(flag)printf("YES\n");
	else printf("NO\n");
	return 0;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

materialistOier

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值