M.Subsequence(南昌网络赛)

本文介绍了一种用于判断字符串是否为另一个字符串子序列的算法。通过遍历目标字符串,检查是否能在源字符串中找到对应的字符顺序,实现子序列的判断。文章提供了一个C++代码示例,演示了如何实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Give a string SS and NN string T_iTi​ , determine whether T_iTi​ is a subsequence of SS.

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

If there is an array \lbrace K_1, K_2, K_3,\cdots, K_m \rbrace{K1​,K2​,K3​,⋯,Km​}so that 1 \le K_1 < K_2 < K_3 < \cdots < K_m \le N1≤K1​<K2​<K3​<⋯<Km​≤N and S_{k_i} = T_iSki​​=Ti​, (1 \le i \le m)(1≤i≤m), then T_iTi​ is a subsequence of SS.

Input

The first line is one string SS,length(SS) \le 100000≤100000

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

Then next nn lines,every line is a string T_iTi​, length(T_iTi​) \le 1000≤1000

Output

Print NN lines. If the ii-th T_iTi​ is subsequence of SS, print YES, else print NO.

样例输入复制

abcdefg
3
abc
adg
cba

样例输出复制

YES
YES
NO

直接暴力模拟就行了,遍历 串的每个字符,在 串中寻找有没有,如果没有的话就返回 false.

代码:

#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s[100005];
char t[10004];
int main()
{
    int n;
    scanf("%s",s);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",t);
        int len=strlen(t);
        int zlen=strlen(s);
        int j=len-1;
        int flog=0;
        if(zlen<len)
        {
            printf("NO\n");
            continue;
        }
        else
        {
            for(int i=zlen-1; i>=0; i--)
            {
                if(t[j]==s[i])
                {
                    //printf("%d %d\n",i,j);
                    //printf("%c %c\n",t[j],s[i]);
                    j--;
                    flog++;
                }
                if(j<0)
                    break;
            }

        }
        if(flog==len)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值