package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
class Trie{
public int flag;
public char ch;
public int leng;
public LinkedList<Trie>next;
}
class save{
public Trie root=new Trie();
public char[] ss=new char[100];
public void BuildTrie(String str){
char s[]=new char[100];
s=str.toCharArray();
int i,j,len=str.length();
Trie t=root;
for(i=0;i<len;i++){
for(j=0;j<t.leng;j++){
if(t.next.get(j).ch==s[i])
break;
}
if(j<t.leng)
t=t.next.get(j);
else{
Trie h=new Trie();
h.ch=s[i];
h.flag=0;
if(t.leng==0)
t.next=newLinkedList<Trie>();
t.next.add(h);
t.leng++;
t=h;
t.leng=0;
}
}
t.flag=1;
};
public void Show(Trie t,int index){
int i;
for(i=0;i<t.leng;i++){
ss[index]=t.next.get(i).ch;
if(t.next.get(i).flag==1){
System.out.println(new String(ss,0,index+1));
}
Show(t.next.get(i),index+1);
}
}
public void set(String name){
root.leng=0;
try{
FileReader one=new FileReader(name);
BufferedReader to=new BufferedReader(one);
String s=to.readLine();
while(s!=null){
BuildTrie(s);
s=to.readLine();
}
Show(root,0);
}
catch(IOException e){};
}
}
public class Main {
public static voidmain(String agrs[]){
Scanner in=newScanner(System.in);
String name;
name=in.next();
save to=newsave();
to.set(name);
}
}
第一次实现,代码估计有点臭,大牛可以不看……