本题将会给出
组测试数据,确切数字未知,您需要一直读入直到文件结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每组测试数据描述如下:
在一行上输入一个长度为
,由可见字符构成的字符串
,代表待判断的密码。
对于每一组测试数据,新起一行。若密码合格,输出
,否则输出
。
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000 021Abc1111
OK NG NG OK OK
对于第二组测试数据,
中存在两个长度大于
的独立子串
,即橙色标记部分。
对于第三组测试数据,仅包含大写字母和数字,不满足条件。
Abc1@ A1@ababa@1A
NG OK
对于第一组测试数据,长度不足
位,不满足条件。
import sys
# 判断密码是否合法
def is_legal(str1):
lst1 = list(str1)
# pop掉最后一个换行符
lst1.pop()
# 长度判断
if len(lst1) <= 8:
return 'NG'
# 公共子串判断
for i in range(len(lst1)-2):
# 长度为3的子串, 重复出现则返回NG
if str1[i:i+3] in str1[i+1:]:
return 'NG'
# 四种情况判断
kind_dict = {'dig': 0, 'upper': 0, 'lower': 0, 'other': 0}
for i in lst1:
# 数字
if i.isdigit():
kind_dict['dig'] = 1
# 大写字母
elif ord('Z') >= ord(i) and ord('A') <= ord(i):
kind_dict['upper'] = 1
# 小写字母
elif ord('z') >= ord(i) and ord('a') <= ord(i):
kind_dict['lower'] = 1
# 其他
else:
kind_dict['other'] = 1
# 满足三种以上
if sum(list(kind_dict.values())) >= 3:
return 'OK'
else:
return 'NG'
# 读取
lines = list(sys.stdin.readlines())
for i in lines:
print(is_legal(i)) import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Yuliang.Lee
* @version 1.0
* @date 2021/8/29 9:46
* 校验密码有效性:
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度大于2的子串重复
示例1
输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK
*/
public class Main {
public static String REGEX_1 = "[A-Z]";
public static String REGEX_2 = "[a-z]";
public static String REGEX_3 = "\\d";
public static String REGEX_4 = "[^A-Za-z0-9]";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String password = in.nextLine();
if (password.length() <= 8) {
System.out.println("NG");
continue;
}
int countRegex = 0;
if (hasRegex(REGEX_1, password)) {
countRegex++;
}
if (hasRegex(REGEX_2, password)) {
countRegex++;
}
if (hasRegex(REGEX_3, password)) {
countRegex++;
}
if (hasRegex(REGEX_4, password)) {
countRegex++;
}
if (countRegex < 3) {
System.out.println("NG");
continue;
}
if (isSubRepeat(password)) {
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
public static boolean hasRegex(String regex, String input) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return true;
}
return false;
}
/**
* 检查是否存在长度为3的多个相同的子串
* @param input
* @return
*/
public static boolean isSubRepeat(String input) {
for (int i = 0; i < input.length() - 3; i++) {
// 如果用模式regex相关的话还需要处理转译字符
if (input.substring(i + 1).contains(input.substring(i, i + 3))) {
return true;
}
}
return false;
}
}
import java.util.*;
public class Main{
public static Boolean checkPassword(String str){
// 验证长度
if(str.length() <= 8) return false;
// 验证包括的字符种类
int low = 0, up = 0, num = 0, other = 0;
for(int i = 0; i < str.length(); i++){
char tmp = str.charAt(i);
if(tmp >= 'a' && tmp <= 'z'){
low = 1;
}
else if(tmp >= 'A' && tmp <= 'Z'){
up = 1;
}
else if(tmp >= '0' && tmp <= '9'){
num = 1;
}
else{
other = 1;
}
}
if(low+up+num+other < 3) return false;
// 验证是否有长度大于2的子串重复
for(int i = 0; i < str.length(); i++){
for(int j = i+3; j < str.length(); j++){
String tmp = str.substring(i, j);
if(str.substring(j).contains(tmp)){
return false;
}
}
}
// 以上false都没有出现,返回true
return true;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.next();
if(checkPassword(str)){
System.out.println("OK");
}
else{
System.out.println("NG");
}
}
}
} let line
const getTypes = password => {
let count = 0;
/[0-9]/.test(password) && count++;
/[a-z]/.test(password) && count++;
/[A-Z]/.test(password) && count++;
/[^A-Za-z0-9]/.test(password) && count++;
return count;
}
const hasRepeat = password => {
let arr = password.split('')
for(let i = 0; i < password.length - 2; i++) {
let subStr = arr.splice(i, 3, ' ').join('')
if(arr.join('').includes(subStr)) return false
arr = password.split('')
}
return true
}
while(line = readline()) {
let length = line.length > 8
let type = getTypes(line) >= 3
let repeat = hasRepeat(line)
let ans = (length && type && repeat) ? 'OK' : 'NG'
print(ans)
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.nextLine();
//条件一
if(str.length() <= 8){
System.out.println("NG");
continue;
}
boolean[] flag = new boolean[4];//判断条件二
HashSet<String> set = new HashSet<>();//判断条件三
boolean is = true;//记录for循环里是否有输出
for(int i = 0;i < str.length();i++){
char c = str.charAt(i);
if(i + 2 < str.length()){
String cur = str.substring(i,i + 3);
if(set.contains(cur)){
System.out.println("NG");
is = false;
break;
}else{
set.add(cur);
}
}
if(c >= '0' && c <= '9'){
flag[0] = true;
}else if(c >= 'a' && c <= 'z'){
flag[1] = true;
}else if(c >= 'A' && c <= 'Z'){
flag[2] = true;
}else{
flag[3] = true;
}
}
if(!is){//for循环里有输出
continue;
}
int count = 0;
for(int i = 0;i < flag.length;i++){
if(flag[i] == false){
count++;
}
}
if(count > 1){
System.out.println("NG");
continue;
}
System.out.println("OK");
}
} import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String passWord;
while((passWord = br.readLine()) != null){
if(judge(passWord))
System.out.println("OK");
else
System.out.println("NG");
}
}
private static boolean judge(String pwd) {
// 长度不足8
if(pwd.length() <= 8) return false;
int lowerAlphaCnt = 0, upperAlphaCnt = 0, digit = 0, other = 0;
for(int i = 0; i < pwd.length(); i++){
if(pwd.charAt(i) >= 'A' && pwd.charAt(i) <= 'Z')
upperAlphaCnt ++;
else if(pwd.charAt(i) >= 'a' && pwd.charAt(i) <= 'z')
lowerAlphaCnt ++;
else if(pwd.charAt(i) >= '0' && pwd.charAt(i) <= '9')
digit ++;
else
other ++;
}
// 字符种类不足三种
if(Math.min(lowerAlphaCnt, 1) + Math.min(upperAlphaCnt, 1) + Math.min(digit, 1) + Math.min(other, 1) < 3)
return false;
// 看是否有相同长度大于2的子串重复
HashSet<String> set = new HashSet<>();
for(int len = 3; len < pwd.length() / 2; len++){
for(int i = 0; i <= pwd.length() - len; i++){
String substr = pwd.substring(i, i + len);
if(!set.contains(substr)){
set.add(substr);
}else{
// 发现已经有这个子串了
return false;
}
}
}
return true;
}
} import java.util.*;
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();
HashMap<Integer,Boolean> map = new HashMap<>(4);
//1.长度超过8位
if(pwd.length() <= 8){
System.out.println("NG");
continue;
}
//2.包括大小写字母.数字.其它符号,以上四种至少三种
for(int i = 0; i < pwd.length(); i++){
char c = pwd.charAt(i);
//有三种了
if(map.size() == 3) break;
//小写字母
if(c >= 'a' && c <= 'z'){
map.put(0,true);
}
//大写字母
else if(c >= 'A' && c <= 'Z'){
map.put(1,true);
}
//数字
else if(c >= '0' && c <= '9'){
map.put(2,true);
}
else{
map.put(3,true);
}
}
if(map.size() < 3) {
System.out.println("NG");
continue;
}
//3. 不能有相同长度大于2的子串重复。
//也就是说在这里,我们就只需要考虑长度为3的子字符串,是否有重复
boolean flag = false;
for(int j = 0; j <= pwd.length() - 6; j++){
String sub = pwd.substring(j , j+3);
if(pwd.indexOf(sub,j+3) != -1) {
flag = true;
break;
}
}
if(flag){
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
}
3.不能有相同长度大于等于2的子串重复
上面测试样本有长度为2的重复, 给的结果还是 "OK"4@M$68(Oh%!n%~9&08&Z@#+dN0&Z
int main()
{
string a;
while(cin>>a)
{
int flat[4]={0};
if(a.size()<=8)
{
cout<<"NG"<<endl;
continue;
}
else
{
int j=0;
for(int i=0;i<a.size();i++)
{
if(isdigit(a[i]))
flat[0]=1;
else if(islower(a[i]))
flat[1]=1;
else if(isupper(a[i]))
flat[2]=1;
else
flat[3]=1;
if(flat[0]+flat[1]+flat[2]+flat[3]==3)
break;
}
if(flat[0]+flat[1]+flat[2]+flat[3]==3)
{
int i=2;
for(;i<a.size()-2;i++)
{
if(a.find(string(a.begin()+i-2,a.begin()+i+1),i+1)!=string::npos)
{
cout<<"NG"<<endl;
break;
}
}
if(i==a.size()-2)
cout<<"OK"<<endl;
}
else
cout<<"NG"<<endl;
}
}
return 0;
} 先检测长度,然后检测字符是否三种以上,然后检测重复,都满足则欧克
#include <stdio.h>
(737)#include <string.h>
#include <ctype.h>
int main()
{
int i,j,n;
char keywords[128]={0};
int num[4]={0};
sc: while(scanf("%s",keywords) != EOF)
{
n = strlen(keywords);
num[0] = num[1] = num[2] = num[3] = 0;
if( n <= 8)
{
printf("NG\n");
continue;
}
for(i=0; keywords[i]!=0; i++)
{
if(islower(keywords[i]) )
num[0]= 1;
else if(isupper(keywords[i]))
num[1]= 1;
else if(isdigit(keywords[i]))
num[2]= 1;
else if(isgraph(keywords[i]))
num[3]= 1;
}
if(num[0]+num[1]+num[2]+num[3] < 3)
{
printf("NG\n");
continue;
}
for(i=0; i+2 < n; i++)
for(j=1; j+2 < n;j++)
if(i!=j && keywords[i] == keywords[j] && keywords[i+1] == keywords[j+1] \
&& keywords[i+2] == keywords[j+2])
{
printf("NG\n");
goto sc;
}
printf("OK\n");
}
return 0;
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String x = sc.nextLine();
int[] flag = new int[4];
if (x.length() <= 8) {
System.out.println("NG");
continue;
}
for (int i = 0; i < x.length(); i++) {
if (x.charAt(i) >= 'a' && x.charAt(i) <= 'z') {
flag[0] = 1;
} else if (x.charAt(i) >= 'A' && x.charAt(i) <= 'Z') {
flag[1] = 1;
} else if (x.charAt(i) >= '0' && x.charAt(i) <= '9') {
flag[2] = 1;
} else {
flag[3] = 1;
}
}
if (flag[0] + flag[1] + flag[2] + flag[3] < 3) {
System.out.println("NG");
continue;
}
boolean valid = true;
for1:
for (int i = 0; i <= x.length() - 6; i++) {
for2:
for (int j = i + 3; j <= x.length() - 3; j++) {
if (x.substring(i, i + 3).equals(x.substring(j, j + 3))) {
System.out.println("NG");
valid = false;
break for1;
}
}
}
if (valid) {
System.out.println("OK");
}
}
}
} #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<sstream>
#include<algorithm>
using namespace std;
bool beyond2(string s)
{
int len=s.length();
int flag=0;
for(int i=0; i<len-3; i++)
{
for(int j=i+3; j<=len-3; j++)
{
string temp1=s.substr(i,3);
string temp2=s.substr(j,3);
if(temp1==temp2)
{
flag=1;
// cout<<"temp1: "<<temp1<<" temp2: "<<temp2<<endl;
break;
}
}
if(flag==1)
break;
}
if(flag==0)
return true;
//cout<<"OK"<<endl;
else
return false;
//cout<<"NG"<<endl;
}
bool beyond8(string s)
{
int len=s.length();
if(len>8)
return true;
return false;
}
bool isAa(string s)
{
int len=s.length();
int sum=0;
int flag0=0,flag1=0,flag2=0,flag3=0,flag4=0;
for(int i=0; i<len; i++)
{
if(s[i]>='a'&&s[i]<='z')
flag0=1;
else if(s[i]>='A'&&s[i]<='Z')
flag1=1;
else if(s[i]>='0'&&s[i]<='9')
flag2=1;
else if(s[i]>='a'&&s[i]<='z')
flag3=1;
else
flag4=1;
}
if((flag0+flag1+flag2+flag3+flag4)>=3)
return true;
return false;
}
int main()
{
string s;
while(getline(cin,s))
{
if(beyond8(s)&&beyond2(s)&&isAa(s))
{
cout<<"OK"<<endl;
}
else
cout<<"NG"<<endl;
}
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int cntF = 0;
boolean uppFlag = false;
boolean lowFlag = false;
boolean numFlag = false;
boolean othFlag = false;
boolean reFlag = false;
String input = sc.nextLine();
if(input.length()<9) {
System.out.println("NG");
continue;
}
char[] chars = input.toCharArray();
for(int i=0; i<chars.length; i++) {
char sinChar = chars[i];
for(int j=i+3; j<chars.length-3; j++) {
if(sinChar==chars[j] && chars[i+1]==chars[j+1] && chars[i+2]==chars[j+2]) {
reFlag = true;
break;
}
}
if(sinChar>='A' && sinChar<='Z') {
uppFlag = true;
} else if(sinChar>='a' && sinChar<='z') {
lowFlag = true;
} else if(sinChar>='0' && sinChar<='9') {
numFlag = true;
} else {
othFlag = true;
}
}
if(uppFlag) cntF++;
if(lowFlag) cntF++;
if(numFlag) cntF++;
if(othFlag) cntF++;
if(cntF < 3) {
System.out.println("NG");
continue;
}
if(reFlag) {
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
} import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.nextLine();
boolean valid = isValid(str);
if(valid == true)
System.out.println("OK");
else
System.out.println("NG");
}
scanner.close();
}
private static boolean isValid(String str){
if (str.length() <= 8)
return false;
int num_low = 0, num_upper = 0, num_digit = 0, num_other = 0;
for(int i = 0 ;i < str.length(); i++){
char c = str.charAt(i);
if(c >= 'a' && c <= 'z'){
num_low=1;
continue;
}
if(c >= 'A' && c <= 'Z'){
num_upper=1;
continue;
}
if(c >= '0' && c <= '9'){
num_digit=1;
continue;
}
else{
num_other=1;
continue;
}
}
if((num_low + num_upper + num_digit + num_other) < 3)
return false;
for(int i=0; i<str.length()-3; i++){
for(int j=i+1; j<str.length()-2; j++){
if(str.charAt(i) == str.charAt(j) && str.charAt(i+1) == str.charAt(j+1)
&& str.charAt(i+2) == str.charAt(j+2)){
return false;
}
}
}
return true;
}
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool RepeatedSubStr(string s, int len)
{
for (int i = 0; i<len - 2; i++)
{
string s1 = s.substr(i, 3);
if (s.find(s1, i + 3) != string::npos)
return false;
}
return true;
}
int main()
{
string s;
vector<string> vec;
while (getline(cin, s))
{
int flag[4] = { 0 };
int len = s.length();
if (len <= 8)
{
vec.push_back("NG");
continue;
}
for (int i = 0; i<len; i++)
{
if (s[i] >= '0'&&s[i] <= '9')
flag[0] = 1;
else if (s[i] >= 'a'&&s[i] <= 'z')
flag[1] = 1;
else if (s[i] >= 'A'&&s[i] <= 'Z')
flag[2] = 1;
else
flag[3] = 1;
}
if (flag[0] + flag[1] + flag[2] + flag[3]<3)
{
vec.push_back("NG");
continue;
}
if (!RepeatedSubStr(s, len))
{
vec.push_back("NG");
continue;
}
vec.push_back("OK");
}
vector<string> ::iterator iter;
for (iter = vec.begin(); iter != vec.end(); iter++)
cout << *iter << endl;
}