Check the string

版权声明:转载时 别忘了注明出处 https://blog.csdn.net/ZCY19990813/article/details/79980657

 

A. Check the string

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.

B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.

You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).

Input

The first and only line consists of a string SS (1≤|S|≤50001≤|S|≤5000). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.

Output

Print "YES" or "NO", according to the condition.

Examples

input

Copy

aaabccc

output

Copy

YES

input

Copy

bbacc

output

Copy

NO

input

Copy

aabc

output

Copy

YES

题意:a这个人有个字符串,里面有a字符,让后给b这个人,b可以向里面添加字符b在a给的字符串后面,然后给c这个人,c可以向里面添加字符c,在b给的字符串后面。
题目中保证最后的字符串至少有一个a或者b,然后让你检查这个字符串是否合法。合法就是输出YES,否则NO。

题解:模拟   合法的要求就是a之前不能有b,c字符,b之前不能有c字符,并且c的个数的等于a的个数或者b的个数。

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<cmath>
#include<string>
#include<deque>
#include <vector>
using namespace std;
typedef long long ll;
#define PI acos(-1)
#define MAX 0x3f3f3f3f
int main()
{
    ll a,b,c,s1=0,s2=0,s3=0,flag=0,i,flag1=0,flag2=0;
    string s;
    cin>>s;
    ll n=s.size();
    for(i=0;i<n;i++)
    {
        if(s[i]=='a')
        {
            if(s2!=0||s3!=0)
            {
                flag=1;
                break;
            }
            s1++;
        }
        if(s[i]=='b')
        {
            if(s1==0||s3!=0)
            {
                flag=1;
                break;
            }
            s2++;
        }
        if(s[i]=='c')
        {
            if(s1==0||s2==0)
            {
                flag=1;
                break;
            }
            s3++;
        }
    }
    if(s1==0||s2==0)
        flag=1;
    if((s1==s3||s3==s2)&&flag==0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;
}

 

阅读更多

没有更多推荐了,返回首页