判断多个字符串是否是另一个字符串(仅含小写字母)的子序列(可以不连续)

解析:可以逆序枚举字符串,用ne[i][j]表示i位置的下一个j+'a’字母的位置
这样在查找的时候就可以使用ne[i][j]来找下一个字母的位置了


import java.io.BufferedReader;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Main {
	static int [][] ne =new int [1000100][30];
	static int 	[] temp	 =new int [30];
    public static void main(String []args) throws IOException {
    	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    	String str1=br.readLine();
    	for(int i=str1.length()-1;i>=0;i--) {							
    		for(int j=0;j<26;j++) {
    			ne[i][j] =temp[j];
    		}
    		temp[(int)(str1.charAt(i)-'a')] =i;
    	}
    	String str=br.readLine();
    	int n=Integer.parseInt(str);
    	while(n--!=0) {
    		str=br.readLine();
    		int t =0;
    		if(str.charAt(0)==str1.charAt(0)) {
    			t++;
    		}
    		for(int i=0;i<str1.length();i++) {
    			
    			int te=ne[i][(int)(str.charAt(t)-'a')];
    			
    			if(te!=0) {
    				i=te-1;
    				t++;	
    			}
    			//System.out.println(te);
    			if(te==0||t==str.length()) {
    				break;
    			}
    		}
    		if(t==str.length()) {
    			System.out.println("Yes");
    		}
    		else {
    			System.out.println("No");
    		}
    	}
    }
}```

posted @ 2019-03-17 12:41  ffgcc  阅读(1079)  评论(0编辑  收藏  举报