算法竞赛入门经典(紫书)第三章——All in All UVA-10340

14 篇文章 0 订阅

题意:
给两个字符串,判断是否能够在第二个字符串中删除 0 个或多个字符得到第一个字符串。

思路:
判断第一个字符串从左往右的字母是否也在第二个字符串中从左往右出现。
实现起来的话就是用一个指针指向第一个字符串的第一个位置,遍历第二个字符串,对第二个字符串的每个字母进行判断是否和当前指针指向的第一个字符串的字母相同,相同的话指针就后移一位。
拿题目的样例进行讲解如下:

样例一能够把第一个字符串(下面的那个字符串)遍历一遍,所以 Yes。

样例二不能把第一个字符串(下面的那个字符串)遍历一遍,所以 No。

样例三能够把第一个字符串(下面的那个字符串)遍历一遍,所以 Yes。

样例四能够把第一个字符串(下面的那个字符串)遍历一遍,所以 No。

注意:
两个字符串的长度题目中并没有给出,一开始我设定的是千,结果 Runtime error,后来改成万,还是 Runtime error。。。后来又加了个 0 ,AC 了。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long LL;

char s[100010];
char t[200010];

int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%s%s", s, t) == 2){
        int lens = strlen(s);
        int lent = strlen(t);
        int p = 0;
        for(int i=0; i<lent && p<lens; i++){
            if(s[p] == t[i]) p++;
        }
        if(p == lens) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

想看书上的源代码的话看这 (^▽^)
https://github.com/aoapc-book/aoapc-bac2nd

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值