当前位置: 代码迷 >> 综合 >> Subsequence
  详细解决方案

Subsequence

热度:104   发布时间:2023-11-22 13:49:32.0

题目链接
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;
}
  相关解决方案