Created
February 20, 2015 22:32
-
-
Save StarOrpheus/ac65710a0b7502ae4540 to your computer and use it in GitHub Desktop.
Fuck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.PrintWriter; | |
import java.lang.reflect.Array; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
class Bor | |
{ | |
public class vertex | |
{ | |
int next[] = new int[26]; | |
boolean leaf; | |
int p; | |
int pch; | |
int link; | |
int go[] = new int[26]; | |
vertex(){} | |
} | |
public vertex t[]; | |
public int k = 1; | |
public void init(int v) | |
{ | |
// Arrays.fill(t[v].next, -1); | |
// Arrays.fill(t[v].go, -1); | |
for(int i = 0; i < 26; i++) | |
{ | |
t[v].next[i] = -1; | |
t[v].go[i] = -1; | |
} | |
t[v].p = -1; | |
t[v].leaf = false; | |
} | |
public int VertexCount() | |
{ | |
return k; | |
} | |
public void add_string(final String s) | |
{ | |
int v = 0; | |
for(int i = 0; i < s.length(); i++) | |
{ | |
int c = (int) s.charAt(i) - 'a'; | |
if(t[v].next[c] == -1) | |
{ | |
init(k); | |
t[v].next[c] = k; | |
t[k].p = v; | |
t[k].pch = c; | |
k++; | |
} | |
v = t[v].next[c]; | |
} | |
t[v].leaf = true; | |
} | |
public int link(int v) | |
{ | |
if(t[v].link == -1) | |
{ | |
if(v == 0 || t[v].p == 0) | |
t[v].link = 0; | |
else t[v].link = go(link(t[v].p), t[v].pch); | |
} | |
return t[v].link; | |
} | |
public int go(int v, int c) | |
{ | |
if(t[v].go[c] == -1) | |
{ | |
if(t[v].next[c] != -1) | |
t[v].go[c] = t[v].next[c]; | |
else if(v == 0) | |
t[v].go[c] = 0; | |
else | |
t[v].go[c] = go(link(v), c); | |
} | |
return t[v].go[c]; | |
} | |
Bor() | |
{ | |
t = new vertex[100000]; | |
init(0); | |
} | |
} | |
class Main | |
{ | |
public static void main(String[] argc) | |
{ | |
Scanner sc = new Scanner(System.in); | |
PrintWriter pw = new PrintWriter(System.out); | |
Bor b = new Bor(); | |
int k = sc.nextInt(); | |
for(; k > 0; k--) | |
{ | |
String s = sc.next(); | |
b.add_string(s); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment