HDU杭电 1039 Easier Done Than Said?(字符串问题)

来源:互联网 发布:港式车仔面图片淘宝 编辑:程序博客网 时间:2024/06/08 17:12
Problem Description
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.
题解:重要的思路都在注释里面,仔细体会,尤其是要注意细节方面,flag的值控制判断。
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<ctype.h>int yuan(char c){if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')return 1;elsereturn 0;}int main(){char str[10000];int i,j,len,flag;while(scanf("%s",str)!=EOF&&(strcmp(str,"end")))  //此时还有限制条件,输入字符串end时结束,相等时,返回0 {flag=1;         //永远保证flag为0时,跳出,不符合 len=strlen(str);for(i=0;i<len;i++){if(yuan(str[i])){                   break;}}if(i>=len)                     //该句作用为若到最后一个字母也没有元音字母,则flag变为0,这样执行到下面一步可直接跳出 flag=0;for(i=1;i<len;i++)   //此时不要从i=0开始,然后下面是+,因为这样eep会越界,避免这种情况应用-,往回比较,不要往后 {if(!flag)   //flag为0时 跳出 break;if(str[i]==str[i-1])          //flag为0时,才会执行此句 {if((str[i]=='e')||(str[i]=='o'))continue;       //如果相等且满足两个连续的数值为e或者o,则一直遍历下一个。如果相等但不为e或者o,就不执行此个判断,直接执行下一句 flag=0;  // 若两个连续的字符,但是不为e或者o,则把flag设为0 ,直接跳出 break;}}for(i=2;i<len;i++)  //同上面判断,此时不要从i=0开始,然后下面是+,因为这样houctuh会越界,避免这种情况应用-,往回比较,不要往后{if(!flag)  //flag 为0时 跳出 break;if(yuan(str[i])&&yuan(str[i-1])&&yuan(str[i-2])){flag=0;      //此时不合格,因为有连续三个元音 break;}if((!yuan(str[i]))&&(!yuan(str[i-1]))&&(!yuan(str[i-2])))    //此时不合格,因为有连续三个辅音 {flag=0; break;}}if(flag)         //flag若为1,符合条件,输出合格 printf("<%s> is acceptable.\n",str);else          //flag为0时,输出不合格 printf("<%s> is not acceptable.\n",str);} return 0;}


0 0
原创粉丝点击