【算法题】藏宝图

牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t,根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序列。注意,子序列不要求在原字符串中是连续的,例如串 abc,它的子序列就有 {空串, a, b, c, ab, ac, bc, abc} 8 种。

输入描述:
每个输入包含一个测试用例。每个测试用例包含两行长度不超过 10 的不包含空格的可见 ASCII 字符串。

输出描述:
输出一行 “Yes” 或者 “No” 表示结果。

输入例子:
x.nowcoder.com
ooo

输出例子:
Yes


#include <iostream>
#include <numeric>
#include<algorithm>
#include <string>
#include<hash_map>
#include <set>
using namespace std;
//#define debug_

void func(string s,string t)
{
    int last(-1);
    int tmp(0);
    for (auto i = 0; i < t.size();++i)
    {
        tmp = find(s.begin()+(last+1), s.end(), t[i]) - s.begin();
        if (tmp == s.size())
        {
            cout << "No" << endl;
            return;
        }
        if (tmp>last)
        {
            last = tmp;
        }
        else
        {
            cout << "No" << endl;
            return;
        }
    }
    cout << "Yes" << endl;
}

int main()
{
    string s;
    string t;

#ifdef debug_
    s = "abcd";
    t = "abcde";
#else
    cin>>s;
    cin >> t;

#endif
    func(s,t);

    return 0;
}

或者:

#include <iostream>
#include <numeric>
#include<algorithm>
#include <string>
#include<hash_map>
#include <set>
using namespace std;
//#define debug_

void func(string s, string t)
{
    int i(0);
    int j(0);
    while ( i<s.size()&&j<t.size() )
    {
        if (s[i]==t[j])
        {
            ++j;
            if (j == t.size())
            {
                cout << "Yes" << endl;
                return;
            }
        }
        ++i;
    }
    cout << "No" << endl; 
}

int main()
{
    string s;
    string t;

#ifdef debug_
    s = "abcd";
    t = "abcde";
#else
    cin >> s;
    cin >> t;

#endif
    func(s, t);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值