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

扫描二维码关注公众号,回复: 2481747 查看本文章

No

思路:利用数组将字符串s,t录入,再通过循环拆分s的每个字母,边拆分边从t中查找是否有相同的字母,如果找到,则用k标记位置并且f++(f为s从t中查找到的字母个数),结束此次查找的循环,再找s的下一个字母,查找位置从k+1开始,避免重复查找。s中的字母全部查找完之后判断f是否等于s的字符串长度,如果相等,则说明s中的字母全都能从t中顺序找到,输出“Yes”,;否则,输出“No”。

以下是详细代码:

#include<stdio.h>
#include<string.h>
int main()
{
	char s[1000],t[100000];
	int m,n,i,j,k,f;
	while(scanf("%s %s",s,t)!=EOF)
	{
		m=strlen(s);
		n=strlen(t);
		f=0;k=0;
		for(i=0;i<m;i++)
		{
			for(j=k;j<n;j++)
			{
				if(s[i]==t[j])
				{
					k=j+1;
					f++;
					break;
				}
			}
		}
		if(f==m)
			printf("Yes\n");
		else
			printf("No\n");
	}
}

未经授权请勿转载,违者必究!

猜你喜欢

转载自blog.csdn.net/weixin_41618712/article/details/81111703
ALL