您的位置:首页 > 其它

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

2018-02-10 21:19 363 查看
题意:

给两个字符串,判断是否能够在第二个字符串中删除 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: