M题:Subsequence
题目链接:
https://nanti.jisuanke.com/t/38232
题目大意:
给你一串字符串,然后n个询问,问输入的字符串是不是这串字符串的字串(可以不相邻,但字母的顺序不能变)
题目大意:
预处理每个在字符串长度的下一个字符的位置,然后,一步步往后推即可。
如 abcdef
a 1 7 7 7 7 7
b 2 2 7 7 7 7
c 3 3 3 7 7 7
d 4 4 4 4 7 7
e 5 5 5 5 5 7
f 6 6 6 6 6 6
a(1)→b(2)→c(3)→d(4)→e(5)→f(6)
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
typedef long long ll;
const int N=100010;
const int INF=0x3f3f3f3f;
char str[N],s[N];
int cnt1[300],cnt2[300];
int nxt[N][30];
int pt[30];
int getstr(char c){
return c-'a'+1;
}
int main(){
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<30;i++) pt[i]=len+1; //初始化
//for(int i=0;i<30;i++) cout<<pt[i]<<" ";
for(int i=0;i<30;i++) nxt[len+1][i]=len+1;//初始化
for(int i=len-1;i>=0;i--){
for(int j=0;j<30;j++)
nxt[i][j]=pt[j]; //记录第i的上一个j在哪
pt[getstr(str[i])]=i; //记录上一个srtp[i]在哪
}
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s);
int l=strlen(s);
int flag=1;
int id=pt[getstr(s[0])];
for(int i=1;i<l;i++){
int d=nxt[id][getstr(s[i])];
if(d>id){
id=d;
}
else{
flag=0;
break;
}
}
if(flag&&id<=len) puts("YES");
else puts("NO");
}
}
大佬题解链接:
https://www.cnblogs.com/widsom/p/10742707.html?tdsourcetag=s_pctim_aiomsg