Subsequence(暴力)

The Preliminary Contest for ICPC China Nanchang National Invitational
题目链接:https://nanti.jisuanke.com/t/38232

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 nnn 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

直接暴力居然可以过?!ヽ(≧□≦)ノ难受了…果然要少用cin、cout?

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
char s[100005],t[1005];
int main()
{
	int n, flag, lens, lent;
	char *pos;
	scanf("%s", s);
	lens = strlen(s);
	scanf("%d", &n);
	while (n--)
	{
		scanf("%s", t);
		lent = strlen(t);
		pos = s;
		flag = 1;
		for (int i = 0; i < lent; i++)
		{
			pos = strchr(pos, t[i]);
			if (pos == NULL)
			{
				flag = 0;
				break;
			}
			pos++;
		}
		if (flag)
			printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

优化的做法(因为之前用cin、cout疯狂T)

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int pos[100005][30];
char s[100005], t[1005];
int main()
{
	scanf("%s",s);
	int len = strlen(s),n,temp,flag;
	for (int i = len; i > 0; i--)
	{
		for (int j = 0; j < 26; j++)
		{
			pos[i - 1][j] = pos[i][j];
		}
		pos[i-1][s[i] - 'a'] = i+1;
	}
	pos[0][s[0] - 'a'] = 1;
	scanf("%d", &n); 
	while (n--)
	{
		scanf("%s", t);
		len = strlen(t);
		flag = 1;
		temp = pos[0][t[0]-'a'];
		for (int i = 1; i < len; i++)
		{
			if (temp <= 0)
			{
				flag = 0;
				break;
			}
			temp = pos[temp-1][t[i]-'a'];
		}
		if (flag && temp != 0)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值