All in All
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 25558 | Accepted: 10280 |
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.
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 subsequence person compression VERDI vivaVittorioEmanueleReDiItalia caseDoesMatter CaseDoesMatter
Sample Output
Yes No Yes No
Source
题意:就是给两个字符串,让你在第二个字符串里面找第一个,但是可以是:第1个字符串中插入若干字母变成第2个字符串,这种情况也算找到了。找到输出Yes,
没找到输出No.
分析:这题很容易定向思维的以为第一串的长度一定小于第二串。其实是要判断的,如果第一个串大于第二个串,呢么串2一定不会包含串1。如果两个串长度
相等,直接比较是否是相同的串,如果是输出Yes,反之输出No。剩下的情况就是串1的长度小于串2了。这种情况,只需要一重for循环就ok了。i=0,从str1[0]
开始,如果找到相等的字符,就i++.如果串2都找完了还没把串1中的字符按其出现顺序找完,就输出No.
感想:1、开始题意理解错了,以为那种其中间隔字母的不算,就是要在串2中找串1。
2、后来又忽略串长的判断。
3、其实只要串1中的字母按照其出现顺序在串2中全部找到,就可以了。
想清楚了就是水题一道啊~~~~~~~~!!!!!!!
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char str1[100010],str2[100010];
int main()
{
while(scanf("%s %s",&str1,&str2)!=EOF)
{
bool flag=false;
int len1=strlen(str1),len2=strlen(str2);
int i=0,j=0;
if(len1 > len2)
{
puts("No");
}
else if(len1 == len2)
{
if(strcmp(str1,str2) == 0)
puts("Yes");
else
puts("No");
}
else
{
int flag=0;
int i=0,j;
for(j=0;str2[j];j++)
{
if(str2[j]==str1[i])
i++;
if(i==len1)
{
flag=1;
break;
}
}
if(flag) puts("Yes");
else puts("No");
}
}
return 0;
}
11945788 | Accepted | 236K | 0MS | 1027B | 2013-08-08 21:18:50 |