|
|
back to boardWA #2 Why a got WA on test 2? This is my code: #include <iostream> #include <algorithm> using namespace std; long hash[5001]; const char* expr = "=+-*/0123456789)("; void Print(const char* msg) { cout << msg; exit(0); } int main() { freopen("input.txt", "r", stdin); int ch, pr = ' ', pr2 = ' '; int state = 0, ostate, ps = 0, bal = 0; while ((ch = getchar()) != EOF) { if (state == 2 && strchr(expr, ch) == 0) Print("NO\n"); //else if (ch == '*' && pr == '(' && state != 1) { ostate = state; state = 1; } else if (ch == '(' && state != 1 && cin.peek() != '*') { ostate = state; state = 2; ++bal; } else if (ch == ')') { if (state == 2) { if (!bal) Print("NO\n"); else { --bal; if (!bal) state = 0; } } else if (!state) Print("NO\n"); else if (pr == '*' && pr2 != '('&& state == 1) state = ostate; } if (!(ch == '(' && cin.peek() == '*')) { pr2 = pr; pr = ch; } } if (!bal && state != 1) Print("YES\n"); else Print("NO\n"); return 0; } Explain please for program bugs Re: WA #2 For example, such test: (*) Right answer: NO Re: WA #2 Yes, my program solved this test! But... Test #2! Re: WA #2 Try this test (*(*) YES Re: WA #2 My solution solved all tests from forumboard, but i am always got WA on test 2. Please, help me!!! Re: WA #2 I've tested your program... Try such test: (**) Your program's output is NO. Re: WA #2 Oh, yes... I'm forgot to post new version of my program #include <iostream> using namespace std; const char* str = "=+-*/0123456789)("; void Print(const char* str) { cout << str; exit(0); } int main() { //freopen("input.txt", "r", stdin); int ch, bal = 0; int state = 0, ostate; while ((ch = getchar()) != EOF) { if (ch == '\n') continue; if (ch == '(') { if (cin.peek() == '*' && state != 1) { ostate = state; state = 1; getchar();
continue; } else if (cin.peek() != '*' && state != 1) { state = 2; ++bal; } //else //if (cin.peek() != '*' && state == 0) //Print("NO\n"); } else if (ch == ')') { if (state == 0) Print("NO\n"); else if (state == 2) { --bal; if (bal < 0) Print("NO\n"); else if (!bal) state = 0; } } else if (ch == '*') { if (cin.peek() == ')') { if (state == 1) { state = ostate; getchar(); } else if (state == 0) Print("NO\n"); } } else if (state == 2) { if (strchr(str, ch) == 0) Print("NO\n"); } } if (bal == 0 && state != 1) Print("YES\n"); else Print("NO\n"); return 0; } Re: WA #2 Posted by Lomir 26 Dec 2006 05:21 I have also WA2, but do not know why... Any new ideas aboubt damn test 2!? I am going crazy... #include <cmath> #include <cctype> #include <cstdio> #include <cstdlib> #include <iostream> #include <vector> #include <deque> #include <string> #include <set> #include <algorithm> #define FOR(i,to) for (int i = 0; i < int(to); ++i) #define SZ(a) a.size() using namespace std; string text; bool coment = false; bool formula = false; bool good = true; int open = 0; int main () { while (feof(stdin)) { char buf[10000]; gets(buf); text += string(buf); } FOR(i,SZ(text)) { if (coment) { if (text[i] == '*' && text[i+1] == ')') { i++; coment = false; } continue; } if (formula) { if (text[i] == '(' && text[i+1] == '*') { i++; coment = true; continue; } if (text[i] == '(') { open++; continue; } if (text[i] == ')') { open--; if (open==0) formula = false; continue; } if (strchr("=+-*/0123456789", text[i]) == NULL) { good = false; break; } continue; } if (text[i] == '(') { if (text[i+1] == '*') { i++; coment = true; continue; }else{ open++; formula = true; continue; } } if (text[i] == ')') { good = false; break; } } if (good && !coment && !formula) printf("YES\n"); else printf("NO\n"); return 0; } Re: WA #2 Posted by EA 3 Sep 2007 13:07 Edited by author 03.09.2007 13:10 Re: WA #2 Posted by SHMAK 3 Jul 2009 03:51 if u have got a problem with test # 2 try this test '(*' correct answer is NO I think that this is not valid.. T.T Edited by author 03.07.2009 03:52 |
|
|