-
题目描述:
-
对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。
是否AC的规则如下:
1. zoj能AC;
2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空;
3. 若azbjc 能AC,则azbojac也能AC,其中a,b,c为N个'o'或者为空;
-
输入:
-
输入包含多组测试用例,每行有一个只包含'z','o','j'三种字符的字符串,字符串长度小于等于1000。
-
输出:
-
对于给定的字符串,如果能AC则请输出字符串“Accepted”,否则请输出“Wrong Answer”。
-
样例输入:
-
zoj ozojo ozoojoo oozoojoooo zooj ozojo oooozojo zojoooo
-
样例输出:
-
Accepted Accepted Accepted Accepted Accepted Accepted Wrong Answer Wrong Answer
一开始忘记标记访问过的。。。- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <memory.h>
- #include <cstdio>
- #include <cstdlib>
- #include <vector>
- #include <set>
- using namespace std;
- set<string> ss;
- bool isaccept(string s){
- if(s == "zoj")
- return true;
- string t = s;
- int l = 0, r = 0, m=0;
- string a,b,c;
- while(t.length()>0 && t[0]=='o'){
- t = t.substr(1, t.length()-1);
- l++;
- }
- while(t.length()>0 && t[t.length()-1]=='o'){
- t = t.substr(0, t.length()-1);
- r++;
- }
- if(t.length()>1 && t[0]=='z' && t[t.length()-1]=='j'){
- int i=1;
- while(i<t.length()-1){
- if(t[i]!='o')
- return false;
- i++;
- m++;
- }
- if(l==r && m==1)
- return true;
- for(int i=0;i<l;++i)
- a+='o';
- for(int i=0;i<r-l;++i)
- c+='o';
- for(int i=0;i<m-1;++i)
- b+='o';
- if(ss.count(a+"z"+b+"j"+c)<=0){
- ss.insert(a+"z"+b+"j"+c);
- return isaccept(a+"z"+b+"j"+c);
- }
- }
- return false;
- }
- int main(){
- //freopen("in.txt", "r", stdin);
- string str;
- while(cin>>str){
- ss.clear();
- if(isaccept(str))
- printf("Accepted\n");
- else
- printf("Wrong Answer\n");
- }
- //fclose(stdin);
- }
本文介绍了一种用于判断特定字符串是否符合预设规则的算法。该算法通过递归方式检查字符串是否由'z','o','j'组成且符合特定格式,如'zoj'或'xzojx'等形式。此外,还提供了C++实现代码示例。
3613

被折叠的 条评论
为什么被折叠?



