【cfA】check the string

/*
http://codeforces.com/contest/960/problem/A

outputstandard 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 S (1 ≤ |S| ≤ 5 000). 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
inputCopy
aaabccc
output
YES
inputCopy
bbacc
output
NO
inputCopy
aabc
output
YES
*/

#include<iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include<map>
#include<algorithm>
using namespace std;
char store[5500];
int len,i,j,k;

map<char,int>mmap;
int judge ()
{
    len = strlen(store);//只能用于字符串,不包括\0
    for (i=0; i<len; i++)
        if(store[i]<store[i-1])return 0;
            mmap[store[i]]++;
    if (mmap['a']==0||mmap['b']==0||mmap['c']==0)
        return 0;
    if (mmap['a']==mmap['c']||mmap['c']==mmap['b'])
        return 1;
    else return 0;
}

int main ()
{
    while (~scanf("%s",store))
    {
        printf(judge()?"YES\n":"NO\n");
    }
    return 0;
}

/*注意细节
如果使用纯粹的逻辑写法 更加容易出问题
函数的返回值一定要有收束(保持逻辑严密)

map是一种自定义下标的数组

*/

猜你喜欢

转载自blog.csdn.net/schiwon/article/details/79858405
cfa