M. Subsequence(问一个字符串是否是另一个串的子序列)

Give a string SS and NN string T_iTi​ , determine whether T_iTi​ is a subsequence of SS.

If ti is subsequence of SS, print YES,else printNO.

If there is an array \lbrace K_1, K_2, K_3,\cdots, K_m \rbrace{K1​,K2​,K3​,⋯,Km​} so that 1 \le K_1 < K_2 < K_3 < \cdots < K_m \le N1≤K1​<K2​<K3​<⋯<Km​≤Nand S_{k_i} = T_iSki​​=Ti​, (1 \le i \le m)(1≤i≤m), then T_iTi​ is a subsequence of SS.

Input

The first line is one string SS,length(SS) \le 100000≤100000

The second line is one positive integer N,N \le 100000N,N≤100000

Then next nn lines,every line is a string T_iTi​, length(T_iTi​) \le 1000≤1000

Output

Print NN lines. If the ii-th T_iTi​ is subsequence of SS, print YES, else print NO.

样例输入复制

abcdefg
3
abc
adg
cba

样例输出复制

YES
YES
NO

check[i][j]数组预处理一下,当前i位置的下一个j字母在哪个位置

队友做的,偷偷贴代码

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define debug printf("PASS IN LINE:%d\n",__LINE__)
#define reg register
int input(){
	int x=0,f=0;char ch=getchar();
	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return f? -x:x;
}

#define N (int)(1e5+7)

char s[N];
int check[N][26];

int main(){
	scanf("%s",s);
	int len=strlen(s);
	for(reg int i=0;i<26;++i) check[len-1][i]=-1;
	
	for(reg int i=len-2;i>=0;--i){
		for(reg int j=0;j<26;++j){
			if(j==s[i+1]-'a') check[i][j]=i+1;
			else check[i][j]=check[i+1][j];
		}
	}

	 /*for(int i=0;i<len;i++){
	 	printf("i:%d\n",i);
	 	for(int j=0;j<26;j++){
	 		cout<<check[i][j]<<" ";
	 	}cout<<endl;
	 }*/ 

	int q=input();
	char s1[1007];

	while(q--){
		scanf("%s",s1);
		int len2=strlen(s1);
		for(int i=0,now=0;;i=check[i][s1[now]-'a']){
			if(i==-1&&now<len2){
				printf("NO\n");
				break;
			}
			if(now==len2){
				printf("YES\n");
				break;
			}
			if(s1[now]==s[i]) now++;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/89428441