HDU_1039Easier Done Than Said?

来源:互联网 发布:单片机工程师 编辑:程序博客网 时间:2024/06/02 02:07
Password security is a tricky thing. Users prefer simple passwords that are easy to remember (like buddy), but such passwords are often insecure. Some sites use random computer-generated passwords (like xvtpzyo), but users have a hard time remembering them and sometimes leave them written on notes stuck to their computer. One potential solution is to generate "pronounceable" passwords that are relatively secure but still easy to remember.

FnordCom is developing such a password generator. You work in the quality control department, and it's your job to test the generator and make sure that the passwords are acceptable. To be acceptable, a password must satisfy these three rules:

It must contain at least one vowel.

It cannot contain three consecutive vowels or three consecutive consonants.

It cannot contain two consecutive occurrences of the same letter, except for 'ee' or 'oo'.

(For the purposes of this problem, the vowels are 'a', 'e', 'i', 'o', and 'u'; all other letters are consonants.) Note that these rules are not perfect; there are many common/pronounceable words that are not acceptable.
 

Input
The input consists of one or more potential passwords, one per line, followed by a line containing only the word 'end' that signals the end of the file. Each password is at least one and at most twenty letters long and consists only of lowercase letters.
 

Output
For each password, output whether or not it is acceptable, using the precise format shown in the example.
 

Sample Input
atvptouibontreszoggaxwiinqeephouctuhend
 

Sample Output
<a> is acceptable.<tv> is not acceptable.<ptoui> is not acceptable.<bontres> is not acceptable.<zoggax> is not acceptable.<wiinq> is not acceptable.<eep> is acceptable.<houctuh> is acceptable.

代码:

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int vowel( char a)//判断字符a是否是元音。{if( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')return 1;return 0;}int consonants(char b[])//判断是否是三个连续的元音或辅音。{int i;int s = strlen(b);for( i = 0; i < s; i++)if( vowel(b[i]) )break;           if(!b[i])return 0;for( i = 0; i < s - 2;i++){if ( vowel(b[i]) && vowel(b[i+1]) && vowel(b[i+2]) || !vowel(b[i]) && !vowel(b[i+1]) && !vowel(b[i+2]))return 0;if( b[i] == b[i+1] && b[i] != 'e' && b[i] != 'o')return 0;}if( b[i] == b[i+1] && b[i] !='e' && b[i] != 'o')//包含连续的两个相同的字符,除了'e'和'o'.return 0;return 1;}int main(){char n[25];while( scanf("%s",n) ){   if( strcmp(n,"end") == 0)      break;if(consonants(n))                  printf("<%s> is acceptable.\n",n);              else                  printf("<%s> is not acceptable.\n",n);  }return 0;}
思路解析:

题目要求很明确:

至少包含一个元音;

不能包含三个连续的元音或者三个连续的辅音;

不能出现两个相同的字符,除了'ee'和'oo'.(因为它们是元音嘛)

构造一vowel()函数判断是否元音构造一consonants()函数,满足上述三个要求。

strcmp()函数两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符遇'\0'为止


0 0
原创粉丝点击