-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.java
53 lines (50 loc) · 1.33 KB
/
Trie.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
public final class Trie{
char[] ch;
Trie[] tr;
int[] count;
Trie(){
ch=new char[26];
count=new int[26];
tr=new Trie[26];
}
static String insertIn(String s,Trie root){
int n=s.length();
for(char c:s.toCharArray()){
n--;
int index=c-'a';
if(root.ch[index]==c){
}
else{
root.ch[index]=c;
}
if(n==0){
root.count[index]++;
if(root.count[index]>1){
return s+(root.count[index]-1);
}
else
return "OK";
}
if(root.tr[index]==null){
root.tr[index]=new Trie();
}
root=root.tr[index];
}
return "OK";
}
public static void main(String[] args) throws Exception{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
Trie t=new Trie();
while(n-->0){
Trie root=t;
String s=br.readLine();
String ans=insertIn(s,root);
System.out.println(ans);
}
}
}