import java.util.*; import java.io.*; /** * Use the Aho-Corasick algorithm to find all positions of all matches. * * @author Finn Lidbetter */ public class asqp_aho_corasick_finn { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String text = br.readLine(); int nPatterns = Integer.parseInt(br.readLine()); HashMap strToIndex = new HashMap<>(); String[] queryStrings = new String[nPatterns]; int[] queryIndices = new int[nPatterns]; ArrayList dedupedPatterns = new ArrayList<>(); int index = 0; for (int i=0; i=results[resultsIndex].length) { sb.append("-1\n"); } else { sb.append((results[resultsIndex][k]+1)+"\n"); } } System.out.print(sb); } } class AhoCorasick { GoNode go; int n; public AhoCorasick(String[] words) { n = words.length; go = new GoNode(); for (int j=0; j q = new LinkedList<>(); for (GoNode node: go.next.values()) q.offer(node); while (!q.isEmpty()) { GoNode r = q.poll(); for (Map.Entry a: r.next.entrySet()) { GoNode s = a.getValue(), t = r.fail; q.offer(s); while (t!=null && !t.next.containsKey(a.getKey())) { t = t.fail; } if (t==null) { t = go; } s.fail = t.next.get(a.getKey()); if (s.fail!=null) { if (s.out==null) { s.out = s.fail.out; } else { OutNode out = s.out; while (out.next!=null) out = out.next; out.next = s.fail.out; } } } } } public int[][] search(String text) { int[] sizes = new int[n]; int[] currIndex = new int[n]; int GET_SIZES = 0; int POPULATE = 1; // Loop over the text twice. In the first loop we determine how much space // will be needed to store the results. In the second loop we store the // results. // Initialize jaggedResults to a dummy size to appease the compiler. int[][] jaggedResults = new int[1][1]; for (int purpose=0; purpose<2; purpose++) { GoNode c = go; for (int i=0; i next = new HashMap<>(); OutNode out = null; GoNode fail = null; }