Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("C:\\clionfolder\\prog4\\input.txt");
  9. ofstream fout("C:\\clionfolder\\prog4\\output.txt");
  10.  
  11. int N;
  12.  
  13. class Letter {
  14. char symbol;
  15. int *map;
  16. public:
  17. Letter(char a) : symbol(a), map(new int[N]) {};
  18.  
  19. ~Letter() {
  20. delete[] map;
  21. }
  22.  
  23. Letter(const Letter &orig) {
  24. this->map = new int[N];
  25. for (int i = 0; i < N; i++) {
  26. this->map[i] = orig.map[i];
  27. }
  28. }
  29.  
  30. char getchar() {
  31. return this->symbol;
  32. }
  33.  
  34. void addstate(int from, int to) {
  35. this->map[from] = to;
  36. }
  37.  
  38. int getstate(int from) {
  39. return this->map[from];
  40. }
  41. };
  42.  
  43. class DKA {
  44. bool *ending;
  45. int start;
  46. vector<Letter> funcmap;
  47. public:
  48. DKA(int k) : ending(new bool[N]), start(k), funcmap({}) {
  49. for (int i = 0; i < N; i++) {
  50. ending[i] = false;
  51. }
  52. }
  53.  
  54. ~DKA() {
  55. delete[] ending;
  56. }
  57.  
  58. DKA(const DKA &orig) {
  59. for (int i = 0; i < N; i++) {
  60. this->ending[i] = orig.ending[i];
  61. }
  62. }
  63.  
  64. int searchfor(char a) {
  65. for (int i = 0; i < this->funcmap.size(); i++) {
  66. if (this->funcmap[i].getchar() == a) {
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72.  
  73. void end(int k) {
  74. this->ending[k] = true;
  75. }
  76.  
  77. void readfunc() {
  78. int from, to;
  79. char a;
  80. fin >> from >> to >> a;
  81. bool unseenbefore = true;
  82. for (int i = 0; i < this->funcmap.size(); i++) {
  83. if (this->funcmap[i].getchar() == a) {
  84. unseenbefore = false;
  85. }
  86. }
  87. int index;
  88. if (unseenbefore) {
  89. this->funcmap.emplace_back(a);
  90. index = this->funcmap.size() - 1;
  91. } else {
  92. index = this->searchfor(a);
  93. }
  94. this->funcmap[index].addstate(from, to);
  95. }
  96.  
  97. int step(int from, char a) {
  98. int index = this->searchfor(a);
  99. if (index == -1) {
  100. throw "Error";
  101. }
  102. return this->funcmap[index].getstate(from);
  103. }
  104.  
  105. bool stringscan(string str, int k) {
  106. for (int i = 0; i < str.length(); i++) {
  107. k = step(k, str[i]);
  108. }
  109. bool l = this->ending[k];
  110. return l;
  111. }
  112. };
  113.  
  114. void dothething() {
  115. int k, f, p, temp, T;
  116. fin >> N >> k >> f;
  117. DKA dka(k);
  118. for (int i = 0; i < f; i++) {
  119. fin >> temp;
  120. dka.end(temp);
  121. }
  122. fin >> p;
  123. for (int i = 0; i < p; i++) {
  124. dka.readfunc();
  125. }
  126. fin >> T;
  127. string str;
  128. for (int i = 0; i < T; i++) {
  129. fin >> str;
  130. if (dka.stringscan(str, k)) {
  131. fout << "YES\n";
  132. } else {
  133. fout << "NO\n";
  134. }
  135. }
  136. }
  137.  
  138. int main() {
  139. if (!fin.is_open())
  140. return 1;
  141. dothething();
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement