牛客网在线编程:藏宝图

题目描述:

牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t,根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序列。注意,子序列不要求在原字符串中是连续的,例如串 abc,它的子序列就有 {空串, a, b, c, ab, ac, bc, abc} 8 种。
输入描述:
每个输入包含一个测试用例。每个测试用例包含两行长度不超过 10 的不包含空格的可见 ASCII 字符串。
输出描述:
输出一行 “Yes” 或者 “No” 表示结果。
示例1
输入

x.nowcoder.com
ooo
输出

Yes

思路:

使用数组存储输入的两个字符串,作为母串s和子串t。使用k记录子串的位置以及已经和母串匹配的数目。
如果k与子串的长度相等则表示匹配成功,输出yes

其实不用ArrayList也可以,String转char类型使用s.charAt[i]即可

网上的思路:

 1 链接:https://www.nowcoder.com/questionTerminal/74475ee28edb497c8aa4f8c370f08c30
 2 来源:牛客网
 3 
 4 int main(){
 5     string str;
 6     while (cin >> str){
 7         string str1;
 8         cin >> str1;
 9         int a=0, b = 0;
10         while (a < str.length()){
11             if (str[a++] == str1[b])
12                 b++;
13  
14         }
15         if (b == str1.length())
16             cout << "Yes" << endl;
17         else
18             cout << "No" << endl;
19     }
20  
21 }
 1 import java.util.*;
 2 public class Cangbaotu {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Scanner sc = new Scanner(System.in);
 7         String s = sc.nextLine();
 8         String t = sc.nextLine();
 9         //System.out.println(s);
10         //System.out.println(t);
11         ArrayList<Character> list = new ArrayList<Character>();
12         ArrayList<Character> key = new ArrayList<Character>();
13         
14         for(int i = 0; i < s.length();i++){
15             list.add(s.charAt(i));
16         }
17         for(int i = 0; i < t.length();i++){
18             key.add(t.charAt(i));
19         }
20         int k = 0;
21         for(int i = 0; i < s.length()&& k < t.length();i++){
22             
23                 if(list.get(i)==key.get(k)) k++;
24             
25 
26         }
27         //System.out.println(k);
28         if(k == t.length()) System.out.println("Yes");
29         else System.out.println("No");
30         
31         
32         
33     }
34 
35 }
原文地址:https://www.cnblogs.com/zlz099/p/8523667.html