Description
A有一些小写英文字母"a"组成的字符串。他把它交给他的朋友B,他把一些字母"b"加到这个字符串的末尾。所以至少有一个"a"和一个"b"存在于字符串中。
现在把这个字符串交给C,他把一些字母"c"加到字符串的结尾。但是,由于C是A和B的好朋友,所以字母"c"的附加数等于“a”的数目或字符串中“b”的数目。字母C的数目也可以同时等于字母"a"的数目和字母"b"的数目。
你有一个字符串在你的手中,检查是否有可能字符串符合上述条件。如果可能,则打印"YES",否则打印"NO"(没有引号)
Input
输入只有一组
第一行为需检测的字符串,字符串长度小于等于5000,保证只含有小写字母’a’, ‘b’, ‘c’
Output
输出"YES"或者"NO"
Sample Input
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Hint
第一组样例中’c’的数量等于’a’的数量
第二组样例中字符串顺序错误
第三组样例中’c’的数量等于’b’的数量
/*
水题,但是自己还是交了 15遍才 AC
需要注意的是:
1、要考虑 a b c 的顺序问题
如 aaccb 是不符合题意的
2、要考虑 a b c 的个数为 0 的情况 !!!
*/
/*用到了 字符串排序 ,用来判断是否顺序不当的情况*/
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
bool cmp(char a,char b)
{
return a<b;
}
int main()
{
int i;
char s[5001];
char b[5001];
int c[5];
int h=0;
int flag=0,flag1=0;
memset(c,0,sizeof(c));
gets(s);
int m=strlen(s);
for(i=0;i<m;i++)
b[i]=s[i];
b[i]='\0';
sort(b,b+m,cmp);
if(strcmp(s,b)!=0)
printf("NO\n");
else{
for(i=0;i<m;i++){
if(s[i]=='a')
c[0]++;
else if(s[i]=='b')
c[1]++;
else
c[2]++;
}
if(c[0]!=0&&c[1]!=0){ // 没考虑的地方!!!!
if(c[2]==c[0]||c[2]==c[1])
printf("YES\n");
else
printf("NO\n");
}
else
printf("NO\n");
}
}