Easier Done Than Said?
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 Inputa
tv
ptoui
bontres
zoggax
wiinq
eep
houctuh
end
Sample Output
问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1039
问题分析:
- 满足三条规则
1.它必须包含至少一个元音。
2.它不能包含三个连续元音或三个连续辅音。
3.除了“ee”或“oo”之外,它不能包含两个连续出现的相同字母。 - 使用一个函数判断是否为元音字母,可用于判断是否满足规则1和2
源代码
import java.util.Scanner;
public class Main {
public static boolean yuanyin(char a) {
//判断是否为元音字母
char b[]= {'a','e','i','o','u'};
for(int i=0;i<5;i++)
if(a==b[i])
return true;
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String string = scanner.next();
if(string.equals("end"))
break;
char a[] = string.toCharArray(); //将字符串转化为字符数组
boolean tag1=false,tag2=true,tag3=true;
for(int i=0;i<string.length();i++) {
if(yuanyin(a[i])) {
tag1=true;
break;
}
}
if(tag1) { //如果包含了元音字母
for(int i=0;i<string.length()-2;i++) {
if(yuanyin(a[i])&&yuanyin(a[i+1])&&yuanyin(a[i+2])) {
//如果是连续三个元音字母
tag2=false;
break;
}
if(!yuanyin(a[i])&&!yuanyin(a[i+1])&&!yuanyin(a[i+2])) {
//如果是连续三个辅音字母
tag2=false;
break;
}
}
}
if(tag1&&tag2) {
//如果满足要求1和2
for(int i=0;i<string.length()-1;i++) {
if(a[i]==a[i+1]) {
//判断是否有连续两个相同的
if(a[i]!='e'&&a[i]!='o') {
//但除了ee和oo
tag3=false;
break;
}
}
}
}
if(tag1&&tag2&&tag3)
//满足3个要求
System.out.println("<"+string+"> is acceptable.");
else
System.out.println("<"+string+"> is not acceptable.");
}
}
}