結果
問題 | No.430 文字列検索 |
ユーザー | uwi |
提出日時 | 2016-10-02 22:31:04 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 9,360 bytes |
コンパイル時間 | 7,558 ms |
コンパイル使用メモリ | 89,864 KB |
実行使用メモリ | 62,600 KB |
最終ジャッジ日時 | 2024-11-10 00:01:13 |
合計ジャッジ時間 | 8,334 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
50,148 KB |
testcase_01 | WA | - |
testcase_02 | AC | 118 ms
56,936 KB |
testcase_03 | AC | 115 ms
56,376 KB |
testcase_04 | AC | 49 ms
49,964 KB |
testcase_05 | AC | 50 ms
50,240 KB |
testcase_06 | AC | 51 ms
50,328 KB |
testcase_07 | WA | - |
testcase_08 | AC | 151 ms
60,788 KB |
testcase_09 | AC | 51 ms
50,088 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
ソースコード
package contest161002; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; import java.util.List; public class N430 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { char[] s = ns().toCharArray(); SuffixAutomatonOfBit sa = SuffixAutomatonOfBit.build(s); sa.lexSort(); int[] oc = sa.enumNumberOfOccurrences(); long ret = 0; for(int Q = ni();Q > 0;Q--){ char[] q = ns().toCharArray(); SuffixAutomatonOfBit.Node node = sa.track(q); if(node != null){ ret += oc[node.id]; } } out.println(ret); } public static class SuffixAutomatonOfBit { public Node t0; public int len; public Node[] nodes; public int gen; public int[] enumNumberOfOccurrences() { int n = gen; int[] dp = new int[n]; for(int i = n-1;i >= 1;i--){ if(nodes[i].original == null)dp[i]++; dp[nodes[i].link.id] += dp[i]; } return dp; } private SuffixAutomatonOfBit(int n) { gen = 0; nodes = new Node[2*n]; this.t0 = makeNode(0, null); } private Node makeNode(int len, Node original) { Node node = new Node(); node.id = gen; node.original = original; node.len = len; nodes[gen++] = node; return node; } public Node track(char[] q) { Node cur = t0; for(char c : q){ cur = cur.getNext(c); if(cur == null)return null; } return cur; } public static class Node { public int id; public int len; public char key; public Node link; private Node[] next = new Node[3]; public Node original; public int np = 0; public int hit = 0; public void putNext(char c, Node to) { to.key = c; if(hit<<~(c-'A')<0){ for(int i = 0;i < np;i++){ if(next[i].key == c){ next[i] = to; return; } } } hit |= 1<<c-'A'; if(np == next.length){ next = Arrays.copyOf(next, np*2); } next[np++] = to; } public boolean containsKeyNext(char c) { return hit<<~(c-'A')<0; // for(int i = 0;i < np;i++){ // if(next[i].key == c)return true; // } // return false; } public Node getNext(char c) { if(hit<<~(c-'A')<0){ for(int i = 0;i < np;i++){ if(next[i].key == c)return next[i]; } } return null; } public List<String> suffixes(char[] s) { List<String> list = new ArrayList<String>(); if(id == 0)return list; int first = original != null ? original.len : len; for(int i = link.len + 1;i <= len;i++){ list.add(new String(s, first - i, i)); } return list; } } public static SuffixAutomatonOfBit build(char[] str) { int n = str.length; SuffixAutomatonOfBit sa = new SuffixAutomatonOfBit(n); sa.len = str.length; Node last = sa.t0; for(char c : str){ last = sa.extend(last, c); } return sa; } public Node extend(Node last, char c) { Node cur = makeNode(last.len+1, null); Node p; for(p = last; p != null && !p.containsKeyNext(c);p = p.link){ p.putNext(c, cur); } if(p == null){ cur.link = t0; }else{ Node q = p.getNext(c); // not null if(p.len + 1 == q.len){ cur.link = q; }else{ Node clone = makeNode(p.len+1, q); clone.next = Arrays.copyOf(q.next, q.next.length); clone.hit = q.hit; clone.np = q.np; clone.link = q.link; for(;p != null && q.equals(p.getNext(c)); p = p.link){ p.putNext(c, clone); } q.link = cur.link = clone; } } return cur; } public SuffixAutomatonOfBit lexSort() { for(int i = 0;i < gen;i++){ Node node = nodes[i]; Arrays.sort(node.next, 0, node.np, new Comparator<Node>() { public int compare(Node a, Node b) { return a.key - b.key; } }); } return this; } public SuffixAutomatonOfBit sortTopologically() { int[] indeg = new int[gen]; for(int i = 0;i < gen;i++){ for(int j = 0;j < nodes[i].np;j++){ indeg[nodes[i].next[j].id]++; } } Node[] sorted = new Node[gen]; sorted[0] = t0; int p = 1; for(int i = 0;i < gen;i++){ Node cur = sorted[i]; for(int j = 0;j < cur.np;j++){ if(--indeg[cur.next[j].id] == 0){ sorted[p++] = cur.next[j]; } } } for(int i = 0;i < gen;i++)sorted[i].id = i; nodes = sorted; return this; } // visualizer public String toString() { StringBuilder sb = new StringBuilder(); for(Node n : nodes){ if(n != null){ sb.append(String.format("{id:%d, len:%d, link:%d, cloned:%b, ", n.id, n.len, n.link != null ? n.link.id : null, n.original.id)); sb.append("next:{"); for(int i = 0;i < n.np;i++){ sb.append(n.next[i].key + ":" + n.next[i].id + ","); } sb.append("}"); sb.append("}"); sb.append("\n"); } } return sb.toString(); } public String toGraphviz(boolean next, boolean suffixLink) { StringBuilder sb = new StringBuilder("http://chart.apis.google.com/chart?cht=gv:dot&chl="); sb.append("digraph{"); for(Node n : nodes){ if(n != null){ if(suffixLink && n.link != null){ sb.append(n.id) .append("->") .append(n.link.id) .append("[style=dashed],"); } if(next && n.next != null){ for(int i = 0;i < n.np;i++){ sb.append(n.id) .append("->") .append(n.next[i].id) .append("[label=") .append(n.next[i].key) .append("],"); } } } } sb.append("}"); return sb.toString(); } public String label(Node n) { if(n.original != null){ return n.id + "C"; }else{ return n.id + ""; } } public String toDot(boolean next, boolean suffixLink) { StringBuilder sb = new StringBuilder("digraph{\n"); sb.append("graph[rankdir=LR];\n"); sb.append("node[shape=circle];\n"); for(Node n : nodes){ if(n != null){ if(suffixLink && n.link != null){ sb.append("\"" + label(n) + "\"") .append("->") .append("\"" + label(n.link) + "\"") .append("[style=dashed];\n"); } if(next && n.next != null){ for(int i = 0;i < n.np;i++){ sb.append("\"" + label(n) + "\"") .append("->") .append("\"" + label(n.next[i]) + "\"") .append("[label=\"") .append(n.next[i].key) .append("\"];\n"); } } } } sb.append("}\n"); return sb.toString(); } // algorithm } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new N430().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }