藏宝图(遍历)

题目描述

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

输入描述:

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

输出描述:

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

输入

x.nowcoder.com
ooo

输出

Yes

 1 import java.util.Scanner;
 2 
 3 /**
 4  * 藏宝图
 5  * @author Dell
 6  *
 7  */
 8 public class Main {
 9 static public String s = "x.nowcoder.com";
10 static public String t = "ooo";
11 static public void f() {
12     int j = 0;
13     for (int i = 0; i < s.length(); i++) {
14         
15         if (s.charAt(i)==t.charAt(j)) {
16             j++;
17         }
18         if (j==t.length()) {
19             System.out.println("Yes");
20             return;
21         }
22     }
23     System.out.println("No");
24 }
25 public static void main(String[] args) {
26     Scanner sc = new Scanner(System.in);
27     s= sc.nextLine();
28     t = sc.nextLine();
29     f();
30 }
31 }
原文地址:https://www.cnblogs.com/the-wang/p/8979298.html