Advertisement
a53

CuvinteAscunse

a53
Apr 26th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <string>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <algorithm>
  7. using namespace std;
  8. #define N_MAX 6
  9. #define M_MAX 6
  10. #define C_MAX 200
  11. #define A_SIZE 26 // alphabet size
  12.  
  13. char tab[N_MAX][C_MAX];
  14. int N, M, C;
  15.  
  16. struct TrieNode
  17. {
  18. // chs: cele 26 de litere din alfabet
  19. TrieNode* chs[A_SIZE];
  20. // count: informatie despre cuvantul de la radcina pana la nod
  21. // -1 cuvantul *nu* e in dictionar
  22. // 0 cuvantul e in dictionar
  23. // > 0 cuvantul e in dictionar si apare de count ori
  24. int count;
  25.  
  26. TrieNode()
  27. {
  28. count = -1;
  29. memset(chs, 0, A_SIZE * sizeof(struct TrieNode*));
  30. }
  31.  
  32. void insert(string cuv);
  33. void print_and_collect(fstream &fout, string cuv, int min_count, vector<int> &counts);
  34. void count_words(int& n_cuv, int min_count);
  35. };
  36.  
  37. void TrieNode::insert(string cuv)
  38. {
  39. TrieNode* tn = this;
  40. int clen = cuv.length();
  41. for (int i = 0; i < clen; i++)
  42. {
  43. int idx = cuv[i] - 'a';
  44. if (tn->chs[idx] == NULL )
  45. {
  46. tn->chs[idx] = new TrieNode();
  47. }
  48. tn = tn->chs[idx];
  49. }
  50. // ultima litera din cuvant
  51. tn->count = 0;
  52. }
  53.  
  54. void TrieNode::count_words(int& n_cuv, int min_count)
  55. {
  56. TrieNode* tn = this;
  57. if (tn == NULL)
  58. return;
  59.  
  60. if (tn->count >= min_count)
  61. {
  62. n_cuv = n_cuv + 1;
  63. }
  64.  
  65. for(int i = 0; i < A_SIZE; i++)
  66. {
  67. tn->chs[i]->count_words(n_cuv, min_count);
  68. }
  69. }
  70.  
  71. void TrieNode::print_and_collect(fstream &fout, string cuv, int min_count, vector<int> &counts) {
  72. TrieNode* tn = this;
  73. if (tn == NULL)
  74. return;
  75. if (tn->count >= min_count)
  76. {
  77. fout << cuv << " " << tn->count << "\n";
  78. counts.push_back(tn->count);
  79. }
  80. for(int i = 0; i < A_SIZE; i++)
  81. {
  82. cuv.push_back('a' + i);
  83. tn->chs[i]->print_and_collect(fout, cuv, min_count, counts);
  84. cuv.erase(cuv.end() - 1, cuv.end());
  85. }
  86. }
  87.  
  88. void print_solution(fstream &fout, TrieNode &dict, int min_count)
  89. {
  90. string s;
  91. vector<int> counts;
  92. int n_words = 0;
  93. dict.count_words(n_words, min_count);
  94. fout << n_words << "\n";
  95.  
  96. dict.print_and_collect(fout, s, min_count, counts);
  97.  
  98. vector<int>::iterator it;
  99. for(it = counts.begin(); it != counts.end(); it++)
  100. {
  101. fout << (char)('a' + (*it % 26)) ;
  102. }
  103. fout << "\n";
  104. }
  105.  
  106. bool in_bounds(int x, int y)
  107. {
  108. return (x >= 0 && y >= 0 && x < N && y < M);
  109. }
  110.  
  111. void dfs(TrieNode *dict, int i, int j)
  112. {
  113. int idx_l = tab[i][j] - 'a';
  114. int idx_r = tab[i][j] - 'a' + 1;
  115. if (tab[i][j] == '?') {
  116. idx_l = 0;
  117. idx_r = A_SIZE;
  118. }
  119.  
  120. int old_ij = tab[i][j];
  121. for(int idx = idx_l; idx < idx_r; idx ++ )
  122. {
  123. tab[i][j] = 'a' + idx;
  124.  
  125. TrieNode* cur_tn = dict->chs[idx];
  126. if(cur_tn == NULL)
  127. continue;
  128.  
  129. // increase current word
  130. if(cur_tn->count >= 0)
  131. {
  132. cur_tn->count++;
  133. }
  134.  
  135. for(int dx = -1; dx < 2; dx++)
  136. {
  137. for(int dy = -1; dy < 2; dy++)
  138. {
  139. if((dx == 0 && dy == 0) || !in_bounds(i + dx, j + dy))
  140. continue;
  141.  
  142. dfs(cur_tn, i + dx, j + dy);
  143. }
  144. }
  145. }
  146. tab[i][j] = old_ij;
  147. }
  148.  
  149. int main()
  150. {
  151. int i, j;
  152. fstream fin, fout;
  153. fin.open("cuvinteascunse.in", fstream::in);
  154. fout.open("cuvinteascunse.out", fstream::out);
  155. fin >> N >> M >> C;
  156. for(i = 0; i < N; i++)
  157. for(j = 0; j < M; j++)
  158. fin >> tab[i][j];
  159.  
  160. TrieNode dict;
  161. string cuv;
  162. for(i = 0; i < C; i++)
  163. {
  164. fin >> cuv;
  165. std::transform(cuv.begin(), cuv.end(),cuv.begin(), ::tolower);
  166. dict.insert(cuv);
  167. }
  168.  
  169. for(i = 0; i < N; i++)
  170. for(j = 0; j < M; j++)
  171. dfs(&dict, i, j);
  172.  
  173. print_solution(fout, dict, 1);
  174.  
  175. fin.close();
  176. fout.close();
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement