[2017校招] 藏宝图

题目描述

牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t,根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序列。注意,子序列不要求在原字符串中是连续的,例如串 abc,它的子序列就有 {空串, a, b, c, ab, ac, bc, abc} 8 种。

输入描述:

每个输入包含一个测试用例。每个测试用例包含两行长度不超过 10 的不包含空格的可见 ASCII 字符串。

输出描述:

输出一行 “Yes” 或者 “No” 表示结果。

示例1

输入

x.nowcoder.com
ooo

输出

Yes

思路:
1.遍历s1串,找与s2有无相等,有则cnt++。
2.s1遍历结束,看看cnt和s2的长度是否相等,相等则是字串。

题解:

 1 #include<cstdio>
 2 using namespace std;
 3 char s1[11];
 4 char s2[11];
 5 int main() {
 6     scanf("%s", s1);
 7     scanf("%s", s2);
 8     int index = 0;
 9     for (int i = 0; s1[i] != '\0'; i++) {
10         if (s1[i] == s2[index]) index++;
11     }
12     if (s2[index] == '\0') printf("Yes");
13     else printf("No");
14     return 0;
15 }

猜你喜欢

转载自www.cnblogs.com/yfzhou/p/9644197.html