样例能过但是全部WA
查看原帖
样例能过但是全部WA
864112
Gengar021127楼主2023/10/23 16:05
import java.util.*;
import java.io.*;

public class Main{

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int N,M;
        N = Integer.parseInt(br.readLine());
        Operation o = new Operation();
        HashSet<String> hash = new HashSet<>();
        for(int i = 1 ; i <= N ; i++){
            String[] STR = br.readLine().split(" ");
            for(int j = 1 ; j < STR.length ; j++){
                o.Insert(STR[j] , i);
            }
        }
        M = Integer.parseInt(br.readLine());
        for(int i = 0 ; i < M ; i++){
            String str = br.readLine();
            if(hash.contains(str)){
                System.out.println();
                continue;
            }
            hash.add(str);
            o.Search(str);
        }
        out.flush();
        out.close();
        br.close();
    }
}

class Operation{
    static Tire root = new Tire();

    public void Insert(String str , int location){
        Tire node = root;
        for(int i = 0 ; i < str.length() ; i++){
            int index = str.charAt(i) - 'a';
            if(node.tire[index] == null){
                node.tire[index] = new Tire();
            }
            node = node.tire[index];
        }
        node.cnt++;
        node.list.add(location);
    }

    public void Search(String str){
        Tire node = root;
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i < str.length() ; i++){
            int index = str.charAt(i) - 'a';
            if(node.tire[index] == null){
                System.out.println();
                return ;
            }
            node = node.tire[index];
        }
        if(node.cnt < 0){
            System.out.println();
            return ;
        }
        for(int i = 0 ; i < node.list.size() ; i++){
            if(i == 0){
                sb.append(node.list.get(i));
            }
            else{
                sb.append(" ").append(node.list.get(i));
            }
        }
        System.out.println(sb);
    }
}

class Tire{
    int cnt;
    Tire[] tire = new Tire[26];
    ArrayList<Integer> list = new ArrayList<>();
}
2023/10/23 16:05
加载中...