import java.util.Scanner;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()){
String password = sc.nextLine();
if(password.length()>8&&!password.contains(" ")&&!password.contains("\n")){
//判断:是否包括大小写字母.数字.其它符号,以上四种至少三种
Boolean checkCharacterFlag = checkCharacter(password);
//判断:不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
Boolean checkRepeatedCharacterFlag = checkRepeatedCharacter(password);
if(checkCharacterFlag&&checkRepeatedCharacterFlag){
System.out.println("OK");
}else {
System.out.println("NG");
}
}else {
System.out.println("NG");
}
}
}
private static Boolean checkCharacter(String password) {
//compile将给定的正则表达式编译为模式。
//matcher创建一个匹配器,匹配给定的输入与此模式。
/*
public Matcher matcher(CharSequence input)创建一个匹配器,匹配给定的输入与此模式。
参数
input - 要匹配的字符序列
结果
这种模式的新匹配器
*/
/*
public static boolean matches (String regex,
CharSequence input)编译给定的正则表达式,并尝试匹配给定的输入。
调用这种方便的方式的形式
Pattern.matches(regex, input);表现方式与表达式完全相同
Pattern.compile(regex).matcher(input).matches()如果一个模式多次被使用,编译一次并重用它将比每次调用此方法更有效。
参数
regex - 要编译的表达式
input - 要匹配的字符序列
结果
正则表达式是否匹配输入
异常
PatternSyntaxException - 如果表达式的语法无效
* */
int count= 0;
if(Pattern.compile("[A-Z]").matcher(password).find()){
count++;
}
if(Pattern.compile("[a-z]").matcher(password).find()){
count++;
}
if(Pattern.compile("[0-9]").matcher(password).find()){
count++;
}
if(Pattern.compile("[^a-zA-Z0-9]").matcher(password).find()){
count++;
}
/*方法2:
Pattern p1 = Pattern.compile("[A-Z]");
if(p1.matcher(password).find()){
count++;
}
Pattern p2 = Pattern.compile("[a-z]");
if(p2.matcher(password).find()){
count++;
}
Pattern p3 = Pattern.compile("[0-9]");
if(p3.matcher(password).find()){
count++;
}
Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");
if(p4.matcher(password).find()){
count++;
}
*/
if(count>=3){
return true;
}else {
return false;
}
}
private static Boolean checkRepeatedCharacter(String password) {
//方法一:
/*
Pattern p = Pattern.compile("^.*(.{3,}).*\\1.*$");
if(p.matcher(password).find()){
return false;
}else {
return true;
}
*/
//方法二:
int count = 0;
for (int i = 0; i < password.length()-3; i++) {
//因为i最后一组的3个字符和J的一样,所以i的最后一组是倒数234,J的最后一组是倒数123.
//所以i < password.length()-3而j <= password.length()-3。
String str01 = password.substring(i,i+3);//substring截取是带头i不带尾i+3。
//System.out.println("---------------------str01="+str01);
for (int j = i+1; j <= password.length()-3; j++) {
String str02 = password.substring(j,j+3);
//System.out.println("str02="+str02);
if (str01.equals(str02)){//注意不能用==。
count++;
return false;
}
}
}
if(count==0){
return true;
}else {
return false;
}
}
}