Chiryen

导航

ZOJ

某年浙大研究生考试的题目.

题目描述:
对给定的字符串(只包含'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>
using namespace std;
int main()
{
    string str;
    int z_pos,j_pos;
    while(cin>>str)
    {
        int len=str.size();
        int z_cnt=0,j_cnt=0;
        for(int i=0;i<len;i++)
        {
            if('z'==str[i])
                z_cnt++;
            if('j'==str[i])
                j_cnt++;
        }
        if(z_cnt!=1||j_cnt!=1)
        {
            cout<<"Wrong Answer"<<endl;
            continue;
        }
        z_pos=str.find('z');
        j_pos=str.find('j');
        while(j_pos-z_pos>2)
        {
            str.erase(j_pos+1,z_pos);
            str.erase(z_pos+1,1);
            z_pos=str.find('z');
            j_pos=str.find('j');
        }

        if(z_pos+j_pos==str.size()-1&&2==j_pos-z_pos)
            cout<<"Accepted"<<endl;
        else
            cout<<"Wrong Answer"<<endl;
    }
    return 0;
}
View Code

 

string::find用法

http://www.cplusplus.com/reference/string/string/find/

 

string::erase用法

http://www.cplusplus.com/reference/string/string/erase/




posted on 2014-01-28 20:04  Chiryen  阅读(256)  评论(0编辑  收藏  举报