Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- class Node{
- const int ALL_CHAR = 26;
- const int MAX_CHAR = 128;
- private static char []map_char_to_int = new char[MAX_CHAR];
- private static char []map_int_to_char = new char[ALL_CHAR];
- private Node[] Character = new Node[ALL_CHAR];
- private int Count = 0;
- public void init(){
- for(char i = 'a'; i <= 'z'; ++i){
- map_char_to_int[i] = (char)(i - 'a');
- map_int_to_char[i - 'a'] = i;
- }
- }
- private int _find(ref string str, int index){
- if(index == str.Length) return ++this.Count;
- if(Character[map_char_to_int[Convert.ToChar(str[index])]] == null)
- Character[map_char_to_int[Convert.ToChar(str[index])]] = new Node();
- return Character[map_char_to_int[Convert.ToChar(str[index])]]._find(ref str, index + 1);
- }
- public int Find(string str){
- str = str.ToLower();
- return _find(ref str, 0);
- }
- private static StringBuilder current = new StringBuilder();
- public void DFS(){
- if(Count > 0) Console.WriteLine("[{0}={1}] ", current, Count);
- for(int i = 0; i < ALL_CHAR; i++){
- if(Character[i] != null){
- current.Append(map_int_to_char[i]);
- Character[i].DFS();
- current.Remove(current.Length - 1, 1);
- }
- }
- }
- }
- class word{
- public static void Main(){
- Node trie = new Node();
- trie.init();
- try{
- StreamReader inf = File.OpenText("sampletext.txt");
- string line = inf.ReadLine();
- while(line != null){
- string []sline = line.Split(' ');
- for(int i = 0; i < sline.Length; i++)
- trie.Find(sline[i]);
- line = inf.ReadLine();
- }
- }catch (Exception e){}
- trie.DFS();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement