All-in-All: Niuke all-in-all
Question:
There are two strings s and t. If you delete some characters from s, connect the remaining characters to get t. T is the subsequence of s.
Develop a program to determine whether t is a subsequence of s.
Input description:
The input contains multiple groups of data. Each group of data contains two strings, s and t.
They all consist of numbers and letters and are less than 100000 in length.
Output description:
Corresponding to each input group. If t is a sub-sequence of s, "Yes" is output; otherwise, "No" is output ".
Input example:
ABC ABC
ABC AB
ABC DE
Output example:
Yes
Yes
No
Code:
package niuke;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); while(read.hasNext()) { String str1 = read.next(); String str2 = read.next(); judgeStr(str1, str2); } read.close(); } /** * * @param source * @param target * @return */ public static int indexOf(char[] source, char[] target) { int targetCount = target.length; int sourceCount = source.length; if (targetCount == 0) { return 0; } char first = target[0]; int max = sourceCount - targetCount; for (int i = 0; i <= max; i++) { if (source[i] != first) { while (++i <= max && source[i] != first) ; } if (i <= max) { int j = i + 1; int end = j + targetCount - 1; for (int k = 1; j < end && source[j] == target[k]; j++, k++) ; if (j == end) { return i; } } } return -1; } public static void judgeStr(String str1, String str2) { int len1 = str1.length(), len2 = str2.length(); int i = 0, j = 0; for(; i<len1 && j<len2;) { if(str1.charAt(i) == str2.charAt(j)) { j ++; } i ++; } if(j == len2) { System.out.println("Yes"); } else { System.out.println("No"); } }}
Note:
The question itself is not difficult, and the focus is on understanding the question purpose.
The indexOf (String, String) method in the Code indicates an incorrect understanding of this question, that is, it must be included in all to output Yes,
For example, "ABCD" "AB" outputs "Yes"
For "ABCD" "AD" output "No"
However, the question itself means that "Yes" must be output for "ABCD" "AD".