习题3-9 子序列 UVa10340

1.题目描述:点击打开链接

2.解题思路:先判断s,t两字符串的长度;然后从s的第一个字符开始,一个个在t中寻找,若找到输出YES,否则输出NO

3.代码:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#define max 100000
char s[max], t[max];

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	while (scanf("%s%s", s, t) != EOF)
	{
		int len1 = strlen(s);
		int len2 = strlen(t);
		if (len1 > len2)
		{
			printf("No\n");
			continue;
		}
		int mark = -1;
		for (int i = 0; i < len1; i++)
		{
			int ok = 0;
			for (int j = mark + 1; j < len2; j++)
			if (s[i] == t[j])
			{
				mark = j;
				ok = 1;
				break;
			}
			if (!ok)
			{
				printf("No\n");
				goto x1;
			}
		}
		printf("Yes\n");
	x1:;
	}
	 return 0;
}

你可能感兴趣的:(uva)