您的位置:首页 > 大数据 > 人工智能

hdu 1039 Easier Done Than Said?

2015-06-27 01:03 513 查看
题意:输入多个字符串,然后逐个判断是否符合要求。

规则一:密码中至少包含一个元音字母;

规则二:它不能包含三个连续的元音或者三个连续的辅音;

规则三:它不能连续出现相同的字母,除了“ee”或“oo”。

import java.util.Scanner;

public class Main {
	public static char[] chs = { 'a', 'e', 'i', 'o', 'u' };

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String s = sc.next();
			if (s.equals("end")) {
				return;
			}
			boolean flag = isAcceptable(s);// 不能改变s的值
			s = ("<" + s + ">");
			if (flag) {
				s += " is acceptable.";
			} else {
				s += " is not acceptable.";
			}
			System.out.println(s);
		}
	}

	public static boolean isAcceptable(String str) {
		if (!isFirstRule(str)) {
			return false;
		}
		if (!isSecondRule(str)) {
			return false;
		}
		if (!isThirdRule(str)) {
			return false;
		}
		return true;
	}

	public static boolean isFirstRule(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (isVowel(s, i)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isVowel(String s, int i) {// 判断是不是元音
		for (int j = 0; j < chs.length; j++) {
			if (s.charAt(i) == chs[j]) {
				return true;
			}
		}
		return false;
	}

	public static boolean isSecondRule(String s) {
		for (int i = 0; i < s.length() - 2; i++) {
			boolean flag = false;
			if (isVowel(s, i)) {
				flag = true;
			}
			// 如果flag是true,那么后面的字母必须全为元音才返回true;
			// 如果flag是false,那么后面的字母必须全为辅音才返回true;
			if (flag == isVowel(s, i + 1)) {
				if (flag == isVowel(s, i + 2)) {
					return false;// 三个元音或者三个辅音
				}
			}
		}
		return true;
	}

	public static boolean isThirdRule(String s) {
		for (int i = 0; i < s.length() - 1; i++) {
			char ch1 = s.charAt(i);
			char ch2 = s.charAt(i + 1);
			boolean flag = false;
			if (ch1 == 'e' || ch1 == 'o') {
				flag = true;
			}
			if (ch1 == ch2) {// 当ch1与ch2是元音时返回true,否则返回false
				return flag;
			}
		}
		return true;// 当ch1ch2不相等时,返回true;
	}
}


Easier Done Than Said?

说起来容易做起来难?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9876 Accepted Submission(s): 4802



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),
一些网站使用随机生成的密码(如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.
FnordCom正在开发这样一个密码生成器。

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'.

它不能连续出现相同的字母,除了“ee”或“oo”。

(For the purposes of this problem, the vowels are 'a', 'e', 'i', 'o', and 'u'; all other letters are consonants.)
(这个问题的范围,元音‘a’,‘e’,‘o’,‘o’,和‘u’,和所有其他辅音字母。)

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.
输入将包含多组数据,一个测试事件一行,如果输入的是”end“就表示输入结束。
Each password is at least one and at most twenty letters long and consists only of lowercase letters.

每个密码的长度大于0小于21(0,21),只包含小写字母。

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

对于每一个测试实例,判断其是否可以接受。输出格式如下。

Sample Input
[code]a
tv
ptoui
bontres
zoggax
wiinq
eep
houctuh
end
[/code]



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.




Source
Mid-Central USA 2000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: