匹配问题
/* Problem D Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 21 Accepted Submission(s) : 3 Problem Description 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。 是否AC的规则如下: 1. zoj能AC; 2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空; 3. 若azbjc 能AC,则azbojac也能AC,其中a,b,c为N个'o'或者为空; Input 输入包含多组测试用例,每行有一个只包含'z','o','j'三种字符的字符串,字符串长度小于等于1000; Output 对于给定的字符串,如果能AC则请输出字符串“Accepted”,否则请输出“Wrong Answer”。 Sample Input zojozojoozoojoooozoojoooozoojozojooooozojozojoooo Sample Output AcceptedAcceptedAcceptedAcceptedAcceptedAcceptedWrong AnswerWrong Answer */ #include<iostream> #include<string.h> #include<stdio.h> #include<stack> using namespace std; int main() { char c[1010]; stack<char>s; stack<char>s1; stack<char>s2; while(cin>>c) { int i=0,tar=1,k1=0,k2=0,sum=0; while(c[i]=='o') s1.push(c[i++]); while(c[i]!='j'&&i<strlen(c)) s.push(c[i++]); s.push(c[i++]); while(i<strlen(c)) s2.push(c[i++]); while(!s1.empty()) { if(s1.top()=='o') k1++; else { tar=0; break; } s1.pop(); } while(!s2.empty()) { if(s2.top()=='o') k2++; else { tar=0; break; } s2.pop(); } if(tar==0) { printf("Wrong Answer\n"); while(!s.empty()) s.pop(); while(!s1.empty()) s1.pop(); while(!s2.empty()) s2.pop(); continue; } while(s.size()>=3&&!s.empty()) { if(s.top()=='j') s.pop(); else break; while(!s.empty()&&s.top()=='o') { sum++; s.pop(); } if(!s.empty()&&s.top()=='z') s.pop(); else tar=0; break; } if(((k1==k2&&sum==1)||(k2==sum*k1))&&s.empty()&&tar==1&&sum>0) printf("Accepted\n"); else printf("Wrong Answer\n"); while(!s.empty()) s.pop(); while(!s1.empty()) s1.pop(); while(!s2.empty()) s2.pop(); } return 0; }