Subsequence

题目链接
Give a string S and N string Ti, determine whether Ti is a subsequence of S.

If ti is subsequence of S, print YES,else print NO.

If there is an array {K1, K2, K3,..., Km} so that 1 <= K1 < K2 < K3 < ... < Km <= N and Ski = Ti, (1≤i≤m), then Ti is a subsequence of S.

Input
The first line is one string S,length(S) ≤100000

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

Then next n lines,every line is a string Ti, length(Ti)≤1000

Output
Print N lines. If the i-th Ti is subsequence of S, print YES, else print NO.

样例输入
abcdefg
3
abc
adg
cba

样例输出
YES
YES
NO

题意模拟即可

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10,M = 1e3 + 10;
char str[N],tmp[M];
int n,k,cnt,len1,len2; 
int main(){//使用cin,cout会超时,加上ios :: sync_with_stdio(false);cin.tie(0),也会超时!!!!
	scanf("%s%d",str,&n);
	len1 = strlen(str);
	for(int i = 0;i < n;i++){
		scanf("%s",tmp);
		k = cnt = 0,len2 = strlen(tmp);//提前算出来长度,放到循环里面会超时,时间复杂度O(n)
		for(int i = 0;i < len2;i++){
			for(;k < len1;k++){
				if(tmp[i] == str[k]){
					cnt++,k++;//k++从下一个开始比对
					break;
				} 
			}
		}
		if(cnt == len2) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值