【字符串】2019ICPC南昌邀请赛M Subsequence(为啥forA了,whileT了_(:з」∠)_)

就是简单的查询字符串t能否由字符串s按序抽出字符组成

然后我一开始用while T了,之后我们就想乱七八糟的方法,存储每个字符在s中的下标,将t第一个字符在的下标记为last,然后在第二个字符中找s中下标大于last的,以此类推能找全就OK ,依旧T

后来队友换了for写,就A了,还是不知道while怎么就T了

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
const int maxn = 1e5 + 10;
char s[maxn], tmp[maxn];
int n;
using namespace std;
int main()
{
    scanf("%s", s);
    cin >> n;

    int len1 = strlen(s);
    for(int k=1;k<=n;++k)
    {
    	scanf("%s", tmp);

        int len2 = strlen(tmp);

        int s1 = 0, s2 = 0;
        for(s1 = 0; s1 < len1; s1++)
        {
            if(s[s1] == tmp[s2])
            {
                s2++;
                if(s2 == len2)
                    break;
            }
        }

        if(s2 == len2)  cout << "YES" << endl;
        else    cout << "NO" << endl;
    }
}

while版本(T)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
const int N=1e5+10;
char s[N];
char t[1100];
using namespace std;
int main()
{
    scanf("%s",s);
    int n;
    scanf("%d",&n);
    int len_S=strlen(s);
    for(int k=1;k<=n;++k)
    {
        scanf("%s",t);
        int len_T=strlen(t);
        //int last=0;
        int i=0,j=0;
        while(i<len_S&&j<len_T)
        {
            if(s[i]==t[j])
                ++j;
            ++i;
        }
        if(j==len_T)   printf("YES\n");//cout<<"YES"<<endl;
        else printf("NO\n");//cout<<"NO"<<endl;

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值