Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <fstream>
  5. #include <queue>
  6. #include <stack>
  7. #include <string>
  8. #include <time.h>
  9. #include <limits>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. //Cтруктура ситуации
  15. struct situation {
  16. string leftPart, RightPart;
  17. int position, index;
  18.  
  19. situation() {
  20. position = 0;
  21. index = 0;
  22. RightPart = "";
  23. }
  24.  
  25. situation(string left, string right, int pos, int ix) {
  26. leftPart = left;
  27. RightPart = right;
  28. position = pos;
  29. index = ix;
  30. }
  31.  
  32. bool operator<(const situation s2) const {
  33. if (leftPart != s2.leftPart){
  34. return (leftPart < s2.leftPart);
  35. }
  36. if (RightPart != s2.RightPart){
  37. return (RightPart < s2.RightPart);
  38. }
  39. if (position != s2.position) {
  40. return position < s2.position;
  41. }
  42. if (index != s2.index) {
  43. return index < s2.index;
  44. }
  45. return false;
  46. }
  47. };
  48. //Расширенные ситуации
  49. struct wide_situation {
  50. string leftPart, RightPart;
  51. int position, index, indexOfD;
  52.  
  53. wide_situation() : position(0), index(0), RightPart("") {
  54. }
  55.  
  56. wide_situation(string left, string right, int pos, int ix, int indexOfDTmp) {
  57. leftPart = left;
  58. RightPart = right;
  59. position = pos;
  60. index = ix;
  61. indexOfD = indexOfDTmp;
  62. }
  63.  
  64. };
  65. //проекция расширенной ситуации
  66. struct pi_situation {
  67. string leftPart, RightPart;
  68. int position;
  69.  
  70. pi_situation() {
  71. position = 0;
  72. RightPart = "";
  73. }
  74.  
  75. pi_situation(string l, string r, int p) {
  76. leftPart = l;
  77. RightPart = r;
  78. position = p;
  79. }
  80.  
  81. pi_situation(wide_situation w) {
  82. pi_situation(w.leftPart, w.RightPart, w.position);
  83. }
  84.  
  85. bool operator<(const pi_situation s2) const {
  86. if (leftPart != s2.leftPart){
  87. return (leftPart < s2.leftPart);
  88. }
  89. if (RightPart != s2.RightPart){
  90. return (RightPart < s2.RightPart);
  91. }
  92. if (position != s2.position) {
  93. return position < s2.position;
  94. }
  95. return false;
  96. }
  97. };
  98.  
  99. class node {
  100. public:
  101. std::stack<pi_situation> stack;
  102.  
  103. node() : q1(false), alreadyRead(0) {
  104. while (!stack.empty()) {
  105. stack.pop();
  106. }
  107. }
  108.  
  109. int alreadyRead;
  110. bool q1;
  111.  
  112. void write() {
  113. cout << "Already read = " << alreadyRead << endl;
  114. std::stack<pi_situation> stackTmp;
  115. while (!stack.empty()) {
  116. pi_situation s = stack.top();
  117. stack.pop();
  118. stackTmp.push(s);
  119. s.write();
  120. }
  121. cout << endl;
  122. while (!stackTmp.empty()) {
  123. stack.push(stackTmp.top());
  124. stackTmp.pop();
  125. }
  126. }
  127. };
  128.  
  129. vector<situation> state[100000000]; //состояния
  130. vector<pair<string, string> > rules; //список правил
  131. set<situation> stateSet[100000000]; //множество состояний
  132. vector<wide_situation> wide_situations; //расширенные состояния
  133. vector<pi_situation> pi_situations; //проекции расширенных состояний
  134. set<pi_situation> pi_situationsSet; //множество расширенных состояний
  135. queue<node> q;
  136. string word;
  137.  
  138. void countMoving(node nodeTMP) {
  139. //(q, a, (A -> x*aB)) -> (q, (A -> xa*B))
  140. node newNode;
  141. newNode = nodeTMP;
  142. if (newNode.alreadyRead < word.length()) {
  143. if (newNode.stack.size() > 0) {
  144. pi_situation current = newNode.stack.top();
  145. if (current.position == 2) {
  146. current.position = 2;
  147. }
  148. if (current.RightPart.length() > current.position && current.RightPart[current.position] == word[newNode.alreadyRead]) {
  149. ++newNode.alreadyRead;
  150. ++current.position;
  151. newNode.stack.pop();
  152. newNode.stack.push(current);
  153. q.push(newNode);
  154. }
  155. }
  156. }
  157. newNode = nodeTMP;
  158. //(q0, ε,(A → α · Bη)) → (q0,(A → α · Bβ)(B → ·β)) ∀ (B → β) ∈ P// я бы поставил n вместо b
  159. for (int i = 0; i < rules.size(); ++i) {
  160. if (nodeTMP.stack.size() > 0) {
  161. pi_situation current = nodeTMP.stack.top();
  162. if (current.RightPart.length() > current.position && current.RightPart[current.position] == rules[i].first[0] && rules[i].first.length() == 1) {
  163. newNode = nodeTMP;
  164. newNode.stack.pop();
  165. pi_situation newsituation = current;
  166. newsituation.leftPart = current.leftPart;
  167. // newsituation.RightPart = current.RightPart.erase((unsigned long) (current.pos + 1));
  168. //newsituation.RightPart += rules[i].second;
  169. newsituation.RightPart = current.RightPart;
  170. newNode.stack.push(newsituation);
  171. newsituation = current;
  172. newsituation.leftPart = rules[i].first;
  173. newsituation.RightPart = rules[i].second;
  174. newsituation.position = 0;
  175. newNode.stack.push(newsituation);
  176. q.push(newNode);
  177. }
  178. }
  179. }
  180.  
  181. //(q0, ε,(A → α · Bη)(B → β·)) → (q0,(A → αB · η))
  182. newNode = nodeTMP;
  183. if (newNode.stack.size() > 1) {
  184. pi_situation currentSecond = newNode.stack.top();
  185. newNode.stack.pop();
  186. if (currentSecond.position == currentSecond.RightPart.size()) {
  187. pi_situation currentFirst = newNode.stack.top();
  188. newNode.stack.pop();
  189. if (currentFirst.RightPart.size() > currentFirst.position &&
  190. currentFirst.RightPart[currentFirst.position] == currentSecond.leftPart[0] && currentSecond.leftPart.length() == 1) {
  191. ++currentFirst.position;
  192. newNode.stack.push(currentFirst);
  193. q.push(newNode);
  194. }
  195. }
  196.  
  197. }
  198.  
  199. //hq0, ε,(S0 → S·)i → hq1, εi
  200. newNode = nodeTMP;
  201. if (newNode.stack.size() > 0) {
  202. pi_situation current = newNode.stack.top();
  203. newNode.stack.pop();
  204. if (current.leftPart == "$" && current.RightPart.size() == current.position) {
  205. newNode.q1 = true;
  206. q.push(newNode);
  207. }
  208. }
  209.  
  210.  
  211. }
  212.  
  213. void countpi_situation() {
  214. for (int i = 0; i < wide_situations.size(); ++i) {
  215. pi_situations.push_back(pi_situation(wide_situations[i]));
  216. pi_situationsSet.insert(pi_situation(wide_situations[i]));
  217. }
  218. }
  219.  
  220. void countwide_situations() {
  221. for (int i = 0; i < state->size(); ++i) {
  222. for (int j = 0; j < state[i].size(); ++j) {
  223. wide_situations.push_back(wide_situation(state[i][j].leftPart, state[i][j].RightPart, state[i][j].position,
  224. state[i][j].index, i));
  225. }
  226. }
  227. }
  228.  
  229.  
  230. void Scan(int j, char c) {
  231. for (int i = 0; i < state[j].size(); i++) {
  232. situation cur = state[j][i];
  233. if (cur.position < cur.RightPart.size() && cur.RightPart[cur.position] == c) {
  234. cur.position++;
  235. state[j + 1].push_back(cur);
  236. }
  237. }
  238. }
  239.  
  240. void Predict(int j) {
  241. for (int i = 0; i < state[j].size(); i++) {
  242. situation cur = state[j][i];
  243. string R = getNonTerminal(cur.RightPart, cur.position);
  244. if (R.size() == 0)
  245. continue;
  246. for (int k = 0; k < rules.size(); k++) {
  247. situation rule = situation(rules[k].first, rules[k].second, 0, j);
  248. if (rules[k].first == R && stateSet[j].count(rule) == 0) {
  249. stateSet[j].insert(rule);
  250. state[j].push_back(situation(R, rules[k].second, 0, j));
  251. }
  252. }
  253. }
  254. }
  255.  
  256. void Complete(int j) {
  257. for (int i = 0; i < state[j].size(); i++) {
  258. situation cur = state[j][i];
  259. if (cur.position != cur.RightPart.size())
  260. continue;
  261. int k = cur.index;
  262. for (int t = 0; t < state[k].size(); t++) {
  263. situation pred = state[k][t];
  264. string R = getNonTerminal(pred.RightPart, pred.position);
  265. if (R != cur.leftPart)
  266. continue;
  267. situation rule = situation(pred.leftPart, pred.RightPart, (int)(pred.position + R.size()), pred.index);
  268. if (stateSet[j].count(rule) == 0) {
  269. stateSet[j].insert(rule);
  270. situation put = pred;
  271. put.position += R.size();
  272. state[j].push_back(put);
  273. }
  274. }
  275. }
  276. }
  277.  
  278. string getNonTerminal(string s, int pos) {
  279. string res = "";
  280. if (pos < s.size() && s[pos] <= 'Z' && s[pos] >= 'A')
  281. res += s[pos++];
  282. while (pos < s.size() && s[pos] >= '0' && s[pos] <= '9')
  283. res += s[pos++];
  284. return res;
  285. }
  286.  
  287. int main() {
  288. ifstream grammar;
  289. grammar.open("grammar.txt");
  290. int n;
  291. grammar >> n;
  292. string S;
  293. for (int i = 0; i < n; i++) {
  294. string left;
  295. string right;
  296. grammar >> left;
  297. getline(grammar, right);
  298. for (int j = 0; j < right.size(); j++)
  299. if (right[j] == ' ') {
  300. right.erase(j, 1);
  301. j--;
  302. }
  303. if (i == 0)
  304. S = left;
  305. rules.push_back(make_pair(left, right));
  306. }
  307.  
  308. rules.push_back(make_pair("$", S));
  309. state[0].push_back(situation("$", S, 0, 0));
  310. stateSet[0].insert(situation("$", S, 0, 0));
  311. grammar.close();
  312.  
  313. getline(cin, word); //введем наше слово
  314. Predict(0);
  315. Complete(0);
  316. for (int i = 0; i < word.size(); i++) {
  317. Scan(i, word[i]);
  318. int oldSize = state[i].size();
  319. Predict(i + 1);
  320. Complete(i + 1);
  321. while (oldSize != state[i].size()) {
  322. oldSize = state[i].size();
  323. Predict(i + 1);
  324. Complete(i + 1);
  325. }
  326. }
  327. node init = node();
  328. //(q, e, e) -> (q, (S' -> S)
  329. init.stack.push(pi_situation("$", S, 0));
  330. bool answer = false;
  331. q.push(init);
  332. time_t timer = time(NULL);
  333. while (!q.empty()) {
  334. //отсечение по времени
  335. if (timer - time(NULL) > CLOCKS_PER_SEC * 10) {
  336. cout << "NO";
  337. return 1;
  338. }
  339. node tmp = q.front();
  340. if (tmp.alreadyRead == 1) {
  341. tmp.alreadyRead = 1;
  342. }
  343.  
  344. //tmp.write(); красивый вывод ситуаций со стека
  345. q.pop();
  346. if (tmp.q1 && tmp.stack.empty() && tmp.alreadyRead == word.length()) {
  347. answer = true;
  348. break;
  349. }
  350. countMoving(tmp);
  351. }
  352. if (stateSet[word.size()].count(situation("$", S, 1, 0)) > 0) {
  353. cout << "YES" << endl;
  354. }
  355. else {
  356. cout << "NO" << endl;
  357. }
  358. if ((stateSet[word.size()].count(situation("$", S, 1, 0)) > 0) == answer) {
  359. cout << "answerCorrect";
  360. }
  361. else {
  362. cout << "fail";
  363. }
  364. return 0;
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement