All in All
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. Input is terminated by EOF.
Output
For each test case output, if s is a subsequence of t.
Sample Input
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter
Sample Output
Yes
No
Yes
题目比较简单,题意大致是给定两个字符串s,t,如果s的字母在t中都出现了,并且是按照s的正序排列的,则输出YES
这题需要注意的是循环控制,如果不对第二个字符串t进行处理将会超时,我这里定义了一个m,m表示检测到相同字母的位置,下一次循环则从m+1检测
#include <cstdio>
#include <cstring>
#define maxn 100000
int main()
{
char s[maxn],t[maxn];
while(scanf("%s%s",s,t)!=EOF)
{
int cnt=0;int m=0;
for(int i=0; i<strlen(s); i++)
{
for(int j=m; j<strlen(t); j++)
{
if(s[i]==t[j])
{
cnt++;
m=j+1;
break;
}
t[j]=' ';
}
}
if(cnt==strlen(s))
printf("Yes\n");
else
printf("No\n");
}
}
本文介绍了一种用于验证一个字符串是否为另一个字符串子序列的有效算法。通过遍历与条件判断,确保了所有字符按顺序出现的同时避免了不必要的重复计算,提高了算法效率。
199

被折叠的 条评论
为什么被折叠?



