Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using std::fill;
  9. using std::string;
  10. using std::vector;
  11.  
  12. const int MAXN = 50000 * 101;
  13.  
  14. int nodes[MAXN][10];
  15. bool leaf[MAXN];
  16.  
  17. void add_to_bor(std::string str, int& last) {
  18. int cur = 0;
  19. for (int i = str.size() - 1; i > -1; i--) {
  20. if (nodes[cur][str[i] - '0'] == 0) {
  21. nodes[cur][str[i] - '0'] = last;
  22. last++;
  23. }
  24. cur = nodes[cur][str[i] - '0'];
  25. }
  26. leaf[cur] = true;
  27. }
  28.  
  29. void task() {
  30. std::ofstream fout("output.txt");
  31. std::ifstream fin("input.txt");
  32. string number = "";
  33. fin >> number;
  34. int count_words = 0;
  35. fin >> count_words;
  36. std::map <char, int> encoding_dict = { {'1', 1 }, {'2' , 2} , {'3' , 3}, {'4' , 4}, {'5' , 5}, {'6' , 6},
  37. {'7' , 7}, {'8' , 8}, {'9' , 9}, {'0' , 0}, {'Q' , 0}, {'W' , 9}, {'E' , 3}, {'R' , 7},{'T' , 8}, {'Y' , 9},
  38. {'U', 8}, {'I' , 1}, {'O' , 0}, {'P' , 7}, {'A' , 2}, {'S' , 7}, {'D' , 3}, {'F' , 3}, {'G' , 4}, {'H' , 4},
  39. {'J' , 1}, {'K' , 5}, {'L' , 5}, {'Z' , 0}, {'X' , 9}, {'C' , 2}, {'V' , 8}, {'B' , 2}, {'N' , 6}, {'M' , 6} };
  40. std::map <string, string> words;
  41. memset(nodes, 0, sizeof(nodes));
  42. memset(leaf, false, sizeof(leaf));
  43. int last = 1;
  44. for (int i = 0; i < count_words; i++) {
  45. string str_word = "";
  46. fin >> str_word;
  47. string str_number = "";
  48. for (int j = 0; j < str_word.size(); j++)
  49. str_number += encoding_dict[str_word[j]] + '0';
  50. add_to_bor(str_number, last);
  51. words[str_number] = str_word ;
  52. }
  53. int* dp = new int[number.size()];
  54. fill(dp, dp + number.size(), 0);
  55. int* index = new int[number.size()];
  56. fill(index, index + number.size(), -2);
  57. for (int i = 0; i < number.size(); i++) {
  58. int cur = 0;
  59. string str = "";
  60. for (int j = i; j > std::max(-1, i - 100); j--) {
  61. if (nodes[cur][number[j] - '0'] == 0)
  62. break;
  63.  
  64. if (leaf[nodes[cur][number[j] - '0']])
  65. if (j != 0) {
  66. if (dp[j - 1] != 0 && dp[i] != 0) {
  67. dp[i] = std::min(dp[j - 1] + 1, dp[i]);
  68. if (dp[i] == dp[j - 1] + 1)
  69. index[i] = j - 1;
  70. }
  71. else if (dp[j - 1] != 0) {
  72. dp[i] = dp[j - 1] + 1;
  73. index[i] = j - 1;
  74. }
  75. }
  76. else {
  77. dp[i] = 1;
  78. index[i] = -1;
  79. }
  80.  
  81. cur = nodes[cur][number[j] - '0'];
  82. }
  83. }
  84. if (dp[number.size() - 1] == 0)
  85. fout << "No solution" << std::endl;
  86.  
  87. else {
  88. int i = number.size() - 1;
  89. vector <string> list_of_number;
  90. while (i > -1){
  91. list_of_number.push_back(words[number.substr(index[i] + 1, i - index[i])]);
  92. i = index[i];
  93. }
  94.  
  95. fout << dp[number.size() - 1] << std::endl;
  96. for (int i = list_of_number.size() - 1; i > -1; i--)
  97. fout << list_of_number[i] + ' ';
  98.  
  99. }
  100.  
  101. fout.close();
  102. fin.close();
  103. }
  104.  
  105. int main() {
  106. task();
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement