本题将会给出
组测试数据,确切数字未知,您需要一直读入直到文件结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每组测试数据描述如下:
在一行上输入一个长度为
,由可见字符构成的字符串
,代表待判断的密码。
对于每一组测试数据,新起一行。若密码合格,输出
,否则输出
。
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000 021Abc1111
OK NG NG OK OK
对于第二组测试数据,
中存在两个长度大于
的独立子串
,即橙色标记部分。
对于第三组测试数据,仅包含大写字母和数字,不满足条件。
Abc1@ A1@ababa@1A
NG OK
对于第一组测试数据,长度不足
位,不满足条件。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while(in.hasNext()){
String password = in.nextLine();
boolean f = true;
if(password.length()<8){
System.out.println("NG");
f = false;
}
int q = 0;
if(password.matches(".*[A-Z].*")) q+=1;
if(password.matches(".*[a-z].*")) q+=1;
if(password.matches(".*[0-9].*")) q+=1;
if(password.matches(".*[^a-zA-Z0-9].*")) q+=1;
if(q < 3) {
System.out.println("NG");
f = false;
}
for(int i = 0; i< password.length() - 4; i++){
String ss = password.substring(i, i+3);
String ls = password.substring(i+3,password.length());
if (ls.contains(ss)) {
System.out.println("NG");
f = false;
break;
}
}
if (f) System.out.println("OK");
}
}
} import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) { // 注意 while 处理多个 case
/**
* 校验密码步骤
* 1 位数>=8
* 2 必须包含大写字母、小写字母、数字、特殊字符中的至少三种
* 3 连续子串不重复
*/
String pwd = in.nextLine();
boolean hasNG = false;
if (pwd.length() < 8) {
System.out.println("NG");
continue;
}
Boolean hasUpper = false;
Boolean hasLower = false;
Boolean hasTs = false;
Boolean hasSz = false;
for (int i = 0;i<pwd.length();i++) {
int asc = (int)pwd.charAt(i);
if ((asc>=33&&asc<=47) || (asc>=58&&asc<=64)|| (asc>=91&&asc<=96)|| (asc>=123&&asc<=126)) {
hasTs = true;
} else if (asc>=48&&asc<=57) {
hasSz = true;
} else if (asc>=65&&asc<=90) {
hasUpper = true;
}else if (asc>=97&&asc<=122) {
hasLower = true;
}
}
Integer hasCount = 0;
if (hasUpper) hasCount++;
if (hasLower) hasCount++;
if (hasTs) hasCount++;
if (hasSz) hasCount++;
if (hasCount<3){
System.out.println("NG");
continue;
}
for (int i = 0;i<pwd.length() - 3;i++) {
String splitStr = pwd.substring(i,i+3);
String[] x = pwd.split(Pattern.quote(splitStr));
if (x.length>2) {
System.out.println("NG");
hasNG = true;
break;
}
}
if (!hasNG) {
System.out.println("OK");
}
}
}
}
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static final String NG = "NG";
private static final String OK = "OK";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s = in.nextLine();
if (s.length() < 8) {
System.out.println(NG);
continue;
}
if (!isMatch(s)) {
System.out.println(NG);
continue;
}
if (isChildStr(s, 0, 3)) {
System.out.println(NG);
continue;
}
System.out.println(OK);
}
}
// 是否匹配 必须包含大写字母、小写字母、数字、特殊字符中的至少三种
private static boolean isMatch(String s) {
int count = 0;
//包含大写字母
if (s.matches(".*[A-Z].*")) {
count++;
}
//小写字母
if (s.matches(".*[a-z].*")) {
count++;
}
//数字
if (s.matches(".*\\d.*")) {
count++;
}
//特殊字符
if (s.matches(".*[^a-zA-Z0-9].*")) {
count++;
}
return count >= 3;
}
// 是否有子串
private static boolean isChildStr(String s, int l, int r) {
if (r >= s.length()) {
return false;
}
String s1 = s.substring(l, r);
String s2 = s.substring(r);
if (s2.contains(s1)) {
return true;
}
return isChildStr(s, l + 1, r + 1);
}
} 看了大神的简洁代码,我再压缩一下。。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String a = in.nextLine();
if (a.length() < 8) {
System.out.println("NG");
} else if (!hasDiffChars(a)) {
// 计算分数,是否满足3分规则
System.out.println("NG");
} else if (hasRepeatChars(a)) {
// 判断重复字串
System.out.println("NG");
} else {
// 通过校验
System.out.println("OK");
}
}
}
public static boolean hasRepeatChars(String a) {
boolean repeat = false;
for (int i = 0; i < a.length() - 4; i++) {
String sub = a.substring(i, i + 3);
String suffix = a.substring(i + 3);
if (suffix.contains(sub)) {
repeat = true;
break;
}
}
return repeat;
}
public static boolean hasDiffChars(String a) {
int k = 0, l = 0, m = 0, n = 0;
for (char c : a.toCharArray()) {
if (isDigitChar(c)) {
k = 1;
} else if (isUpperChar(c)) {
l = 1;
} else if (isLowerChar(c)) {
m = 1;
} else {
// 其他三种都不是,算特殊字符
n = 1;
}
}
return k+l+m+n >= 3;
}
public static boolean isUpperChar(char c) {
return c >= 65 && c <= 90;
}
public static boolean isLowerChar(char c) {
return c >= 97 && c <= 122;
}
public static boolean isDigitChar(char c) {
return c >= 48 && c <= 57;
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<String> list=new ArrayList<String>();
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
list.add(in.nextLine());
}
for (String s : list) {
System.out.println(getOk(s,0,3));
}
}
public static String getOk(String str,int l,int r){//最小字串
if(str.length()<8) return "NG";
int count=0;
//str是否包含大写字母 小写字母 数字 特殊字符 中的三种及以上
if (str.matches(".*[A-Z].*")) count++;
if (str.matches(".*[a-z].*")) count++;
if (str.matches(".*[0-9].*")) count++;
if (str.matches(".*[^A-Za-z0-9].*")) count++;
if(count<3) return "NG";
for (int i = 0; i < str.length() - r; i++) {
if (str.substring(i+r,str.length()).contains(str.substring(l+i,r+i))) return "NG";
}
return "OK";
}
} import java.util.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 循环读取输入直到没有更多的行
while (scanner.hasNext()) {
String input = scanner.next();
// 检查密码长度是否大于8位
if (input.length() <= 8) {
System.out.println("NG");
continue;
}
// 检查密码是否包含至少三种不同类型的字符
if (getMatch(input)) {
System.out.println("NG");
continue;
}
// 检查密码中是否有长度大于2的重复子串
if (getString(input, 0, 3)) {
System.out.println("NG");
continue;
}
// 如果所有检查都通过,则输出OK
System.out.println("OK");
}
// 关闭scanner对象
scanner.close();
}
// 检查密码是否包含至少三种不同类型的字符
private static boolean getMatch(String str) {
// 初始化计数器
int count = 0;
// 编译正则表达式模式
Pattern p1 = Pattern.compile("[A-Z]"); // 大写字母
Pattern p2 = Pattern.compile("[a-z]"); // 小写字母
Pattern p3 = Pattern.compile("[0-9]"); // 数字
Pattern p4 = Pattern.compile("[^a-zA-Z0-9]"); // 特殊字符
// 检查密码是否包含大写字母
if (p1.matcher(str).find()) {
count++;
}
// 检查密码是否包含小写字母
if (p2.matcher(str).find()) {
count++;
}
// 检查密码是否包含数字
if (p3.matcher(str).find()) {
count++;
}
// 检查密码是否包含特殊字符
if (p4.matcher(str).find()) {
count++;
}
// 如果密码包含至少三种不同类型的字符,则返回false(不满足NG条件)
return count < 3;
}
// 检查密码中是否有长度大于2的重复子串
private static boolean getString(String str, int l, int r) {
// 如果右指针超出字符串长度,则返回false(没有重复子串)
if (r >= str.length()) {
return false;
}
// 检查从左指针到右指针的子串是否在右指针之后的位置出现
if (str.substring(r).contains(str.substring(l, r))) {
return true;
} else {
// 如果没有找到重复子串,则向右移动右指针并递归检查
return getString(str, l + 1, r + 1);
}
}
} import java.util.Scanner;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String passwd = new String();
while (sc.hasNext()) {
passwd = sc.nextLine();
//第一步,判断密码长度,密码少于等于8位直接输出NG重新循环
if (passwd.length() <= 8) {
System.out.println("NG");
continue;
}
//第二步判断复杂度
if (!regCheck(passwd)) {
System.out.println("NG");
continue;
}
//第三步无连续重复字符串判断,包括0个但不包括3个
if(!dupCheck(passwd,0,3)){
System.out.println("NG");
continue;
}else System.out.println("OK");
}
}
//连续重复字符串判断
private static boolean dupCheck(String passwd, int s, int e) {
//如果索引e已经大于等于密码长度,表明没有连续重复字符串,返回真
if (e >= passwd.length()) {
return true;
}
//如果从索引e开始的子串包含从索引s到索引e的子串,表明有重复连续字符串,返回假
if (passwd.substring(e).contains(passwd.substring(s, e))) {
return false;
} else {
//将起始索引和结束索引加1,重新进行判断
return dupCheck(passwd,s+1,e+1);
}
}
//密码复杂段判断
private static Boolean regCheck(String passwd) {
//分别写四个正则式,代表包含数字、小写字母、大写字母以及除了前三个以外的所有字符
Pattern p1 = Pattern.compile("[0-9]");
Pattern p2 = Pattern.compile("[a-z]");
Pattern p3 = Pattern.compile("[A-Z]");
Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");
int count = 0;
if (p1.matcher(passwd).find()) count++;
if (p2.matcher(passwd).find()) count++;
if (p3.matcher(passwd).find()) count++;
if (p4.matcher(passwd).find()) count++;
if (count >= 3) return true;
return false;
}
} import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
List<String> str = new CopyOnWriteArrayList<>();
while (in.hasNext()) { // 注意 while 处理多个 case
str.add(in.nextLine());
}
for (String s : str) {
if (s.length() <= 8 ) {
System.out.println("NG");
continue;
}
int i = 0;
boolean flag = true ;
while (flag && i < s.length() - 6) {
for (int j = 3 ; j <= (s.length() - i) / 2; j++) {
if (kmp(s.substring(j + i), s.substring(i, j + i))) {
flag = false ;
break;
}
}
i++;
}
if (!flag) {
System.out.println("NG");
continue;
}
int count = 0 ;
if (s.matches(".*[0-9].*")) {
count ++ ;
}
if (s.matches(".*[a-z].*")) {
count ++ ;
}
if (s.matches(".*[A-Z].*")) {
count ++ ;
}
if (s.matches(".*[\\u00A0\\s\"`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?].*")) {
count ++ ;
}
if (count > 2) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
/**
kop算法
*/
private static boolean kmp(String str1, String str2) {
int[] next = new int[str2.length()];
for (int i = 1, j = 0; i < str2.length(); i++) {
if (str2.charAt(i) == str2.charAt(j)) {
j++;
next[i] = j;
} else if (str2.charAt(i) != str2.charAt(j)) {
j = 0;
}
}
for (int i = 0 ; i <= str1.length() - str2.length() ; i++) {
for (int j = 0 ; j < str2.length() ; j++) {
if (str1.charAt(i) == str2.charAt(j)) {
i++;
if (j == str2.length() - 1) {
return true;
}
} else {
i = i - next[j];
break;
}
}
}
return false;
}
} import java.util.Scanner;
import java.util.regex.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
//检查位数
String pwd = in.nextLine();
if (pwd == null || pwd.length() <= 8) {
System.out.println("NG");
return;
}
//检测格式
String[] patterns = new String[] {
"\\d+", "[a-z]+", "[A-Z]+", "[^\\sA-za-z0-9]+"
};
int okCount = 0;
for (int i = 0; i < patterns.length; i++) {
Pattern pattern = Pattern.compile(patterns[i]);
if (pattern.matcher(pwd).find()) {
// System.out.println(patterns[i] + "通过");
okCount += 1;
}
}
if (okCount < 3) {
System.out.println("NG");
return;
}
//检查重复子串
boolean noReplace = true;
for (int i = 0; i < pwd.length() - 3; i++) {
String s = pwd.substring(i, i + 3);
int checkIndex = i + 1;
while(checkIndex< pwd.length() - 3){
if(pwd.indexOf(s,checkIndex)!=-1){
noReplace = false;
break;
}
checkIndex++;
}
}
System.out.println(noReplace?"OK":"NG");
}
}
} import java.util.Scanner;
public class PasswordVerify {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String password = scanner.nextLine();
isValid(password);
}
}
private static void isValid(String password) {
if (password.length() < 8) {
System.out.print("NG");
return;
}
char[] letter = password.toCharArray();
int upperCaseLetter = 0;
int lowercaseLetter = 0;
int number = 0;
int other = 0;
for (char c : letter) {
if (c >= 'a' && c <= 'z') {
lowercaseLetter = 1;
} else if (c >= 'A' && c <= 'Z') {
upperCaseLetter = 1;
} else if (c >= '0' && c <= '9') {
number = 1;
} else {
other = 1;
}
}
if (upperCaseLetter + lowercaseLetter + number + other < 3) {
System.out.println("NG");
} else {
boolean isValid = true;
for (int i = 0; i < letter.length - 3; i++) {
for (int j = i + 1; j <= letter.length - 3; j++) {
if (letter[i] == letter[j] && letter[i + 1] == letter[j + 1] && letter[i + 2] == letter[j + 2]) {
isValid = false;
break;
}
}
}
if (isValid) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
}
import java.util.HashSet;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String input = in.nextLine();
if(input.length()<=8) {
System.out.println("NG");
continue;
}
boolean[] condition = new boolean[4];
for (int i=0; i<input.length(); i++) {
char c = input.charAt(i);
if (c>='a' && c<='z') {
condition[0] = true;
continue;
}
if (c>='A' && c<='Z') {
condition[1] = true;
continue;
}
if (c>='0' && c<='9') {
condition[2] = true;
continue;
}
if (c!=' ' && c!='\t') {
condition[3] = true;
continue;
}
}
int count = 0;
for (boolean tmp:condition) {
if (tmp) ++count;
}
if (count < 3) {
System.out.println("NG");
continue;
}
boolean isNG = false;
for (int j=0; j<input.length()-3; j++) {
String sub = input.substring(j,j+3);
String remain = input.substring(j+3);
if (remain!=null && remain.contains(sub)) {
isNG = true;
break;
}
}
if (isNG) {
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
} import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args)throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=br.readLine())!=null){
check(line);
}
}
private static boolean check(String str) {
// 长度超过8位
if (str.length() < 8) {
System.out.println("NG");
return false;
}
// 包括至少3种
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
count |= 1 << 0;
} else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
count |= 1 << 1;
} else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
count |= 1 << 2;
} else {
count |= 1 << 3;
}
}
if (Integer.bitCount(count) < 3) {
System.out.println("NG");
return false;
}
// 不能有长度大于2的包含公共元素的子串重复,子串长度至少3
for (int i = 0; i < str.length() - 3; i++) {
String sub = str.substring(i, i + 3);
if (str.indexOf(sub, i + 3) != -1) {
System.out.println("NG");
return false;
}
}
System.out.println("OK");
return true;
}
}