Advertisement
Guest User

Challenge 77 Difficult

a guest
Jul 18th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. module main;
  2. import std.stdio, std.algorithm, std.file, std.conv, std.datetime, std.string;
  3.  
  4. struct node {
  5.     node*[26] letters; //a-z in alphabet, not ascii numbered order
  6.     bool terminal; //A complete word finishes in this node
  7. }
  8.  
  9. void branchnode(node* current, string word, uint pos) {
  10.     if(pos == word.length) { //Finished the current word
  11.         current.terminal = true;
  12.         return;
  13.     }
  14.     if(current.letters[word[pos] - 97] == null) //If no node then add one
  15.         current.letters[word[pos] - 97] = new node;
  16.     branchnode(current.letters[word[pos] - 97], word, pos + 1);
  17. }
  18.  
  19. string[] trieSearch(char[][] bog, int y, int x, string s, bool[][] block, node* current) {
  20.     block[y][x] = true; //Block against retracing steps
  21.     s ~= bog[y][x]; //Add square's letter to current string
  22.     string[] words = current.terminal? [s] : []; //Complete word?
  23.  
  24.     //Test the 8 adjacent squares for the continuation of the word
  25.     foreach(i;[[y + 1, x], [y - 1, x], [y, x + 1], [y + 1, x + 1], [y - 1, x + 1], [y, x - 1], [y - 1, x - 1], [y + 1, x - 1]])
  26.         if(i[0] >= 0 && i[0] < bog.length && i[1] >= 0 && i[1] < bog[i[0]].length) //In bounds
  27.             if(block[i[0]][i[1]] == false && current.letters[bog[i[0]][i[1]] - 97] != null)
  28.                 words ~= trieSearch(bog, i[0], i[1], s, block, current.letters[bog[i[0]][i[1]] - 97]);
  29.  
  30.     block[y][x] = false;
  31.     return words;
  32. }
  33.  
  34. void main() {
  35.     StopWatch sw, search;
  36.     sw.start;
  37.  
  38.     node* start = new node;
  39.     foreach(word; read("enable1.txt").to!string.split) //Create trie structure
  40.         branchnode(start, word, 0);
  41.  
  42.     string[] letters = "T N L E P I A C N M
  43.                        T R E H O C F E I W
  44.                        S C E R E D A M E A
  45.                        A O M O G O O F I E
  46.                        G A C M H N L E R X
  47.                        T D E R J T F O A S
  48.                        I T A R T I N H N T
  49.                        R N L Y I V S C R T
  50.                        A X E P S S H A C F
  51.                        I U I I I A I W T T".toLower.split;
  52.  
  53.     char[][] bog;
  54.     foreach(i;0..10) {
  55.         ++bog.length;
  56.         foreach(j;i * 10..i * 10 + 10)
  57.             bog[$ - 1] ~= letters[j];
  58.     }
  59.  
  60.     //100x100 version
  61.     foreach(ref line;bog) {
  62.         char[] temp;
  63.         foreach(i;0..10)
  64.             temp ~= line;
  65.         line = temp;
  66.     }
  67.  
  68.     char[][] temp = bog;
  69.     foreach(i;0..9)
  70.         bog ~= temp;
  71.  
  72.     string[] words;
  73.     bool[][] block;
  74.     foreach(line;bog)
  75.         block ~= new bool[line.length];
  76.  
  77.     sw.peek.msecs.writeln("msecs setup time");
  78.     search.start;
  79.  
  80.     foreach(i, line;bog)
  81.         foreach(j, letter;line)
  82.             if(start.letters[bog[i][j] - 97] != null)
  83.                 words ~= trieSearch(bog, i, j, "", block, start.letters[bog[i][j] - 97]);
  84.  
  85.     string[] maxwords = [""];
  86.     foreach(word;words)
  87.         if(word.length > maxwords[0].length)
  88.             maxwords = [word];
  89.         else if(word.length == maxwords[0].length)
  90.             maxwords ~= word;
  91.  
  92.     search.peek.msecs.writeln("msecs search time");
  93.     sw.peek.msecs.writeln("msecs total time");
  94.  
  95.     maxwords.sort.uniq.writeln;
  96.     maxwords[0].length.writeln;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement