Guest User

Untitled

a guest
Jun 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. char graph[120][120];
  5. char words[50010][60];
  6. char number[120] = "";
  7. int used[120];
  8. int n;
  9. int w;
  10. // a, b, c, d, e, f, g, h, i, j, k, l ,m, n, o, p, q, r, s, t, u, v, w, x, y, z
  11. int keys[26] = {2, 2, 2, 3, 3, 3, 4, 4, 1, 1, 5, 5, 6, 6, 0, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};
  12.  
  13. void addEdge (int from, int to, int id) {
  14. //std::cout << from << "->" << to << std::endl;
  15. graph[from][to] = id;
  16. }
  17.  
  18. void toNumbers(char * pattern, int m) {
  19. for(int i = 0; i < m; i ++)
  20. pattern[i] = '0' + keys[pattern[i] - 'a'];
  21. }
  22.  
  23. void KMP (char * pattern, int id) {
  24. int p[100];
  25. p[0] = -1;
  26. int m = std::strlen(pattern);
  27. //toNumbers(pattern, m);
  28. //PF
  29. for(int q = 1; q <= m; q ++) {
  30. p[q] = p[q - 1] + 1;
  31. while(p[q] > 0 && pattern[q - 1] != pattern[p[q] - 1])
  32. p[q] = p[p[q] - 1] + 1;
  33. }
  34. //Match
  35. for(int i = 0, q = 0; i < n; i ++) {
  36. while(q > 0 && ('0' + keys[pattern[q] - 'a']) != number[i])
  37. q = p[q];
  38. if(('0' + keys[pattern[q] - 'a']) == number[i])
  39. q ++;
  40. if(q == m) {
  41. addEdge(i - m + 1, i + 1, id);
  42. q = p[q];
  43. }
  44. }
  45. }
  46. struct qi {
  47. int num, depth, from, prev;
  48. qi(int num_, int depth_, int from_, int prev_) {
  49. num = num_; depth = depth_;
  50. from = from_; prev = prev_;
  51. }
  52. qi() {num = depth = 0; from = -1; prev = -1;}
  53. };
  54. void answer() {
  55. for(int i = 0; i < n; i ++)
  56. used[i] = 0;
  57. qi * queue = new qi[w], curr;
  58. int high = 1, low = 0;
  59. queue[0] = qi();
  60. used[0] = 1;
  61. while(low < high) {
  62. curr = queue[low++];
  63. if(curr.num == n) break;
  64. for(int i = 0; i <= n; i ++) {
  65. if(graph[curr.num][i] != -1 && !used[i]) {
  66. queue[high++] = qi(i,curr.depth + 1,curr.num, low - 1);
  67. used[i] = 1;
  68. }
  69. }
  70. }
  71. if(curr.num != n) {
  72. std::cout << "No solution.";
  73. } else {
  74. int _n = curr.depth, *ans;
  75. try{
  76. ans = new int[_n];}
  77. catch(...){
  78. std::cout << "WTF!?";
  79. }
  80. do {
  81. ans[curr.depth - 1] = graph[curr.from][curr.num];
  82. curr = queue[curr.prev];
  83. }while(curr.depth != 0);
  84. for(int i = 0; i < _n; i ++)
  85. std::cout << words[ans[i]] << " ";
  86. delete[] ans;
  87. }
  88. std::cout << std::endl;
  89. }
  90.  
  91. void main() {
  92. #ifndef ONLINE_JUDGE
  93. freopen("input.txt", "rt", stdin);
  94. freopen("output.txt", "wt", stdout);
  95. #endif
  96. std::ios_base::sync_with_stdio(false);
  97. while(std::cin >> number) {
  98. if(number[0] == '-')
  99. break;
  100. n = std::strlen(number);
  101. for(int i = 0; i <= n; i ++) {
  102. for(int j = 0; j <= n; j ++)
  103. graph[i][j] = -1;
  104. }
  105. std::cin >> w;
  106. for(int i = 0; i < w; i ++) {
  107. std::cin >> words[i];
  108. KMP(words[i], i);
  109. }
  110. answer();
  111. }
  112. }
Add Comment
Please, Sign In to add comment