Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <iterator>
  7. using namespace std;
  8.  
  9. class Node{
  10. public:
  11. char ch;
  12. vector<Node*> children;
  13. bool isWord;
  14. Node(char c) : ch(c), isWord(false), children(0) {}
  15. };
  16.  
  17. class BOR{
  18. Node* root;
  19. public:
  20. BOR() :root(new Node('-')){}
  21. void add(string str)
  22. {
  23. Node* node = root;
  24.  
  25. bool flag = false;
  26. for (int i = 0; i < str.length();)
  27. {
  28. char cur = str[i];
  29. for (int j = 0; j < node->children.size() && !flag; j++)
  30. {
  31. if (node->children[j]->ch == cur)
  32. {
  33. node = node->children[j];
  34. // a=(Node*)node->children[j];
  35. i++;
  36. flag = true;
  37. break;
  38. }
  39. }
  40. if (!flag)
  41. {
  42. Node* tmp = new Node(cur);
  43. // node->children.push_back(tmp);
  44. i++;
  45. }
  46. }
  47. }
  48.  
  49. };
  50. struct Word{
  51. string letters;
  52. string digits;
  53. Word(string str)
  54. {
  55. letters = str;
  56. transcript();
  57. }
  58. Word(){}
  59.  
  60. void transcript()
  61. {
  62. for (int i = 0; i < letters.length(); i++)
  63. {
  64.  
  65. char temp = letters[i];
  66. if (temp == 'I' || temp == 'J' || temp == '1')
  67. digits += '1';
  68. if (temp == 'A' || temp == 'B' || temp == 'C' || temp == '2')
  69. digits += '2';
  70. if (temp == 'D' || temp == 'E' || temp == 'F' || temp == '3')
  71. digits += '3';
  72. if (temp == 'G' || temp == 'H' || temp == '4')
  73. digits += '4';
  74. if (temp == 'K' || temp == 'L' || temp == '5')
  75. digits += '5';
  76. if (temp == 'M' || temp == 'N' || temp == '6')
  77. digits += '6';
  78. if (temp == 'P' || temp == 'R' || temp == 'S' || temp == '7')
  79. digits += '7';
  80. if (temp == 'T' || temp == 'U' || temp == 'V' || temp == '8')
  81. digits += '8';
  82. if (temp == 'W' || temp == 'X' || temp == 'Y' || temp == '9')
  83. digits += '9';
  84. if (temp == 'O' || temp == 'Q' || temp == 'Z' || temp == '0')
  85. digits += '0';
  86. }
  87. }
  88. friend bool operator <(Word const & a, Word const & b)
  89. {
  90. return a.digits < b.digits;
  91. }
  92. };
  93.  
  94. vector<Word> dictionary;
  95. string number = "";
  96. int dictionaryLength;
  97. vector<int> counter;
  98. vector<string> words;
  99. BOR b;
  100.  
  101. void inizialize(ifstream& in)
  102. {
  103. in >> number;
  104. in >> dictionaryLength;
  105.  
  106. dictionary.resize(dictionaryLength);
  107. for (int i = 0; i < dictionaryLength && !in.eof(); i++)
  108. {
  109. string str;
  110. in >> str;
  111. b.add(str);
  112. dictionary[i] = Word(str);
  113. }
  114.  
  115. counter.resize(number.length());
  116. fill(counter.begin(), counter.end(), 1e9);
  117. words.resize(number.length());
  118. fill(words.begin(), words.end(), "");
  119.  
  120. }
  121.  
  122. bool compare(string s1, string s2)
  123. {
  124. if (s2.length() < s1.length())
  125. return false;
  126. string a1 = s1.substr(0, s1.length());
  127. string a2 = s2.substr(0, s1.length());
  128. if (a1 == a2)
  129. return true;
  130. return false;
  131. }
  132.  
  133. vector<Word>::iterator find(vector<Word>::iterator& start, vector<Word>::iterator& end, string str){
  134. vector<Word>::iterator it;
  135. vector<Word>::iterator result = dictionary.end();
  136. for (it = start; it != end; it++)
  137. {
  138. if ((*it).digits == str)
  139. {
  140. result = it;
  141. start = it;
  142. break;
  143. }
  144. }
  145. for (it; it != end && compare(str, (*it).digits); it++);
  146. end = it;
  147. return result;
  148. }
  149.  
  150. vector<string> solve()
  151. {
  152. vector<Word>::iterator it;
  153. vector<Word>::iterator start;
  154. vector<Word>::iterator end;
  155. for (int i = number.size()-1; i >=0; i--)
  156. {
  157. start = dictionary.begin();
  158. end = dictionary.end();
  159. for (int j = 0; j <number.size() - i-1; j++)
  160. {
  161. string str = number.substr(i, j+1);
  162. if (words[i+j+1] != "")
  163. {
  164. it = find(start, end, str);
  165. if (it != dictionary.end())
  166. if (counter[i+j+1] + 1 < counter[i]){
  167. words[i] = (*it).letters;
  168. counter[i] = counter[i+j+1]+1;
  169. }
  170. }
  171. }
  172. it = find(start, end, number.substr(i, number.size()-i));
  173. if (it!=dictionary.end()){
  174. words[i] = (*it).letters;
  175. counter[i] = 1;
  176. }
  177. }
  178. return words;
  179. }
  180.  
  181.  
  182.  
  183.  
  184. void main(){
  185. ifstream in("input.txt");
  186. ofstream out("output.txt");
  187.  
  188.  
  189.  
  190.  
  191. inizialize(in);
  192.  
  193. sort(dictionary.begin(), dictionary.end());
  194.  
  195. vector<string> r = solve();
  196. string result = "";
  197. int c = 0;
  198. int k = 1;
  199.  
  200. if (r[c] == "")
  201. out << "No solution";
  202. else{
  203. result = r[c];
  204. c += r[c].length();
  205. while (c <r.size())
  206. {
  207. if (r[c] != ""){
  208. result += " " + r[c];
  209. c += r[c].length();
  210. k++;
  211. }
  212. else c--;
  213. }
  214. out << k << endl;
  215. out << result;
  216. }
  217. in.close();
  218. out.close();
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement