Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin ("regex.in");
  7. ofstream fout ("regex.out");
  8.  
  9. int n, q0, i, len, nod, auxnod, j;
  10. char s[1010];
  11.  
  12. void stea(int &i) {
  13. fout << nod << " . " << nod + 1 << "\n";
  14. fout << nod << " . " << nod + 3 << "\n";
  15. fout << nod + 1 << ' ' << s[i] << ' ' << nod + 2 << "\n";
  16. fout << nod + 2 << " . " << nod + 3 << "\n";
  17. fout << nod + 3 << " . " << nod + 1 << "\n";
  18. i += 2;
  19. nod += 3;
  20. }
  21.  
  22. void normal(int &i) {
  23. fout << nod << ' ' << s[i] << ' ' << nod + 1 << "\n";
  24. i++;
  25. nod++;
  26. }
  27.  
  28. int main() {
  29. fin >> s;
  30. nod = 1;
  31. q0 = 1;
  32. i = 0;
  33. len = strlen(s);
  34. while (i < len) {
  35. if ('a' <= s[i] && s[i] <= 'z') {
  36. if (s[i + 1] == '*') {
  37. stea(i);
  38. } else {
  39. normal(i);
  40. }
  41. } else {
  42. i++;
  43. j = i;
  44. while (s[j] != ')' && s[j] != '|') {
  45. j++;
  46. }
  47. if (s[j] == ')') { // cazul cu * la final de paranteza
  48. auxnod = nod;
  49. fout << nod << " . " << nod + 1 << "\n";
  50. nod++;
  51. while (i < j) {
  52. if ('a' <= s[i] && s[i] <= 'z') {
  53. if (s[i + 1] == '*') {
  54. stea(i);
  55. } else {
  56. normal(i);
  57. }
  58. }
  59. }
  60. fout << nod << " . " << nod + 1 << "\n";
  61. nod++;
  62. fout << auxnod << " . " << nod << "\n";
  63. fout << nod << " . " << auxnod + 1 << "\n";
  64. i += 2;
  65. } else { // cazul |
  66. while (s[j] != ')') {
  67. j++;
  68. }
  69. if (s[j + 1] == '*') { // * dupa paranteza cu |
  70. int cap1;
  71. auxnod = nod;
  72. fout << nod << " . " << nod + 1 << "\n";
  73. nod++;
  74. while (s[i] != '|') {
  75. if (s[i + 1] == '*') {
  76. stea(i);
  77. } else {
  78. normal(i);
  79. }
  80. }
  81. fout << nod << " . " << auxnod << "\n";
  82. i++;
  83. fout << auxnod << " . " << nod + 1 << "\n";
  84. nod++;
  85. while (s[i] != ')') {
  86. if (s[i + 1] == '*') {
  87. stea(i);
  88. } else {
  89. normal(i);
  90. }
  91. }
  92. fout << nod << " . " << auxnod << "\n";
  93. nod++;
  94. i += 2;
  95. fout << auxnod << " . " << nod << "\n";
  96. } else { // fara * dupa paranteza cu |
  97. auxnod = nod;
  98. while (s[i] != '|') {
  99. if (s[i + 1] == '*') {
  100. stea(i);
  101. } else {
  102. normal(i);
  103. }
  104. }
  105. i++;
  106. if (s[i + 1] == '*') {
  107. if (s[i + 2] == ')') {
  108. fout << auxnod << " . " << nod + 1 << "\n";
  109. fout << auxnod << " . " << nod << "\n";
  110. fout << nod + 1 << ' ' << s[i] << ' ' << nod + 2 << "\n";
  111. fout << nod + 2 << " . " << nod << "\n";
  112. fout << nod << " . " << nod + 1 << "\n";
  113. i += 2;
  114. auxnod = nod;
  115. nod += 3;
  116. } else {
  117. fout << auxnod << " . " << nod + 1 << "\n";
  118. fout << auxnod << " . " << nod + 3 << "\n";
  119. fout << nod + 1 << ' ' << s[i] << ' ' << nod + 2 << "\n";
  120. fout << nod + 2 << " . " << nod + 3 << "\n";
  121. fout << nod + 3 << " . " << nod + 1 << "\n";
  122. i += 2;
  123. auxnod = nod;
  124. nod += 3;
  125. }
  126. } else {
  127. if (s[i + 1] == ')') {
  128. fout << auxnod << ' ' << s[i] << ' ' << nod << "\n";
  129. auxnod = nod;
  130. i++;
  131. } else {
  132. fout << auxnod << ' ' << s[i] << ' ' << nod + 1 << "\n";
  133. auxnod = nod;
  134. nod++;
  135. i++;
  136. }
  137. }
  138. if (s[i] == ')') {
  139. i++;
  140. } else {
  141. while (s[i + 1] != ')') {
  142. if (s[i + 1] == '*') {
  143. stea(i);
  144. } else {
  145. normal(i);
  146. }
  147. }
  148. fout << nod << ' ' << s[i] << ' ' << auxnod << "\n";
  149. fout << auxnod << " . " << nod + 1 << "\n";
  150. nod++;
  151. i += 2;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement