OpenJudge_1936:All in All
2015-04-18 19:29 星星之火✨🔥 阅读(170) 评论(0) 收藏 举报
描述 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. 输入 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. 输出 For each test case output "Yes", if s is a subsequence of t,otherwise output "No". 样例输入 sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter
样例输出 Yes
No
Yes
No
|
解法:
#include<stdio.h>
#include<string.h>
#define MAXN 100000
char first[MAXN+10];
char second[MAXN+10];
int main(void)
{
int first_len, second_len, i, j;
bool find;
while(scanf("%s%s", first, second) != EOF)
{
first_len = strlen(first);
second_len = strlen(second);
if(first_len > second_len)
printf("No\n");
else
{
j = 0;
for(i = 0; i < first_len; i++)
{
find = false; // 主要用来应付最后一个字符
for(; j < second_len; j++)
{
if(second[j] == first[i])
{
j++;
find = true;
break;
}
}
if(j == second_len)
break;
}
if(find && i >= first_len-1)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
开始的时候,Yes 和No 都写成了大写,一直WA,郁闷了个数小时。。。
更简洁的写法:
#include<stdio.h>
#include<string.h>
#define MAXN 100000
char first[MAXN+10];
char second[MAXN+10];
int main(void)
{
int i, j, first_len, second_len;
while(scanf("%s%s", first, second) != EOF)
{
first_len = strlen(first);
second_len = strlen(second);
i = j = 0;
while(i < first_len && j < second_len)
{
if(first[i] == second[j])
{
i++;
j++;
}
else
j++;
}
if(i == first_len)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}