http://ac.jobdu.com/problem.php?pid=1006

题目描述:
对给定的字符串(只包含'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

一开始忘记标记访问过的。。。
[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <algorithm>  
  4. #include <memory.h>  
  5. #include <cstdio>  
  6. #include <cstdlib>  
  7. #include <vector>  
  8. #include <set>  
  9. using namespace std;  
  10. set<string> ss;  
  11. bool isaccept(string s){  
  12.     if(s == "zoj")  
  13.         return true;  
  14.     string t = s;  
  15.     int l = 0, r = 0, m=0;  
  16.     string a,b,c;  
  17.     while(t.length()>0 && t[0]=='o'){  
  18.         t = t.substr(1, t.length()-1);  
  19.         l++;  
  20.     }  
  21.     while(t.length()>0 && t[t.length()-1]=='o'){  
  22.         t = t.substr(0, t.length()-1);  
  23.         r++;  
  24.     }  
  25.     if(t.length()>1 && t[0]=='z' && t[t.length()-1]=='j'){  
  26.         int i=1;  
  27.         while(i<t.length()-1){  
  28.             if(t[i]!='o')  
  29.                 return false;  
  30.             i++;  
  31.             m++;  
  32.         }  
  33.       
  34.         if(l==r && m==1)  
  35.             return true;  
  36.   
  37.         for(int i=0;i<l;++i)  
  38.             a+='o';  
  39.         for(int i=0;i<r-l;++i)  
  40.             c+='o';  
  41.         for(int i=0;i<m-1;++i)  
  42.             b+='o';  
  43.   
  44.         if(ss.count(a+"z"+b+"j"+c)<=0){  
  45.             ss.insert(a+"z"+b+"j"+c);  
  46.             return  isaccept(a+"z"+b+"j"+c);  
  47.         }  
  48.   
  49.           
  50.     }  
  51.     return false;  
  52.       
  53.   
  54. }  
  55. int main(){  
  56.   
  57.     //freopen("in.txt", "r", stdin);  
  58.     string str;  
  59.     while(cin>>str){  
  60.         ss.clear();  
  61.         if(isaccept(str))  
  62.             printf("Accepted\n");  
  63.         else  
  64.             printf("Wrong Answer\n");  
  65.     }  
  66.     //fclose(stdin);  
  67. }     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值