Link:
http://acm.hdu.edu.cn/showproblem.php?pid=1039
很水的一道题,却一直WA,想了很久以后试试把string改成了字符数组A了。。
思路:对每个字母进行如下操作:
1.用变量b记录元音个数,最后非零则满足rule1。
2.用变量a1,a2分别记录连续的元辅音,若当前字母为元音,则a1++,a2=0。反之亦然。达到3则不符合rule 2。
3.和上一个字母作比较,若相等且不是ee或oo则跳出不符合rule 3。
4.用bool值acceptable记录是否符合,为false时跳出循环。
代码:
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
char word[30];
while(cin>>word && strcmp(word,"end")!=0)
{
char pre = ' ';//last character, initialize it as an impossible value
bool accpetable = true;
int a1 = 0,a2 = 0;//count consecutive vowels/consonants
int b = 0;//count vowels
int len = strlen(word);
for(int i=0; i<len; i++)
{
//rule 3
char cur = word[i];
if(cur==pre && cur!='e' && cur!='o')
{
accpetable = false;
break;
}
pre = cur;
//rule 2
if(cur=='a' || cur=='e' || cur=='i' || cur=='o' || cur=='u')
{
a1++;
a2 = 0;
b++;//rule 3
}
else
{
a2++;
a1 = 0;
}
if(a1>=3 || a2>=3)
{
accpetable = false;
break;
}
}
if(!accpetable || b==0)
printf("<%s> is not acceptable.\n",word);
else
printf("<%s> is acceptable.\n",word);
}
return 0;
}