子序列(All in All, UVa 10340)
来源:互联网 发布:手机淘宝怎么删除评价 编辑:程序博客网 时间:2024/05/09 14:19
Description
You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into
the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.
Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.
Input
The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace.The length of s and t will no more than 100000.
Output
For each test case output “Yes”, if s is a subsequence of t,otherwise output “No”.
Sample Input
sequence subsequenceperson compressionVERDI vivaVittorioEmanueleReDiItaliacaseDoesMatter CaseDoesMatter
Sample Output
YesNoYesNo
本题附上两个版本的代码,分别是从a遍历b和从b遍历a,显然从b遍历一遍a是最简单的,由于我第一次用的是从a遍历b,遍历的次数是strlen(a),还有几个潜在的bug,所以第二种解决方法比较好,还有注意数组开的大一些,10^6比较好,一下附上本人渣代码.
#include <iostream>#include <string.h>using namespace std;int main(){ char a[100000] = { '\0' }, b[100000] = { '\0' }; while (cin >> a >> b) { int j = 0, k = 0; int lena = strlen(a), lenb = strlen(b); for (int i = 0; i < lena; i++) { while (b[j] != '\0') { if (a[i] == b[j]) { k++; j++; break; } j++; } } if (k == lena) { cout<<"Yes\n"; } else { cout<<"No\n"; } } return 0;}
第二种解决方法
#include <iostream>#include <string.h>#include <string>#include <fstream>#include <algorithm>#include <stdio.h>using namespace std;#define MAXN 100002int main(){ char a[MAXN],b[MAXN]; while(cin>>a>>b) { int lena=strlen(a); int lenb=strlen(b); int cur=0; for(int i=0;i<lenb;i++) { if(cur==lena)break; if(b[i]==a[cur]){cur++;} } if(cur!=lena)printf("No\n"); else printf("Yes\n"); } return 0;}
- 子序列(All in All, UVa 10340)
- UVA 10340 All in All 子序列判定 贪心
- UVa 10340 All in All(子序列)
- UVa - 10340 - All in All(暴力遍历、子序列)
- 子序列(All in All)
- 小紫书 习题 3-9(UVA 10340)子序列(All in All)
- 算法竞赛入门-习题3-9 子序列(All in All, UVa 10340)
- 习题3-9 子序列(All in All, UVa 10340)
- UVa 10340 - All in All
- UVa 10340 - All in All
- uva 10340 - All in All
- uva 10340 - All in All
- UVa 10340 - All in All
- UVA 10340 All in All
- UVa 10340 - All in All
- uva 10340 all in all
- UVa 10340 - All in All
- Uva 10340 ALL IN ALL
- 第X大的数 (sdut oj)
- 小鑫爱运动 (sdut oj)
- c++自制小游戏(3rd)getch与system("cls")
- C语言实验——数组逆序 (sdut oj)
- Android端调用Caffe模型实现CNN分类
- 子序列(All in All, UVa 10340)
- python网络数据采集学习笔记:第二章
- CMD中Pushd和Popd命令的用法
- 卡特兰数 学习笔记
- 冒泡排序中数据交换的次数 (sdut oj)
- POJ3252:Round Numbers(数位dp)
- [LeetCode]405. Convert a Number to Hexadecimal
- Openmeetings构建指令
- C语言实验——数日子 (sdut oj)