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;
}