参考了两个大佬的题解:
1)多组strIng数据输入
string str;
while(getline(cin,str)){
}
2)对于题目中三个约束条件的处理
a. len==str.size( );长度小于9,输出NG
b. 定义数组a[4]来统计大写字母,小写字母,数组,其他字符是否出现,遍历a[4]数组相加求和,和小于3,输出NG
c.判断是否存在长度大于2的重复子串,则可以直接判断长度为3的子串有无重复,采用了STL库中的unordered_set以及取str长度为3的子串 ,若(set.empty()||set.find(tmp)==set.end())插入新的子串到set中,否则说明之前出现过重复子串,则置标准位repeat为true;3)通过对长度,出现次数,重复flag进行判断输出NG还是OK。
#include<bits/stdc++.h>
using namespace std;
string Password_Verification(string str){
int len = str.size();//获取字符串长度
int a[4]={0};//初始化一个数组,用0,1记录每一种字符出现与否的情况
int count=0; //计数器,记录字符串包含多少中字符
bool repeat =false; //有相同长度超过2的子串重复的字符串对数
unordered_set<string> set;
//记录字符串是否包含四种字符的情况
for(int i=0;i<len;i++){
if((str[i]>='A')&&(str[i]<'Z')){
a[0]=1;
}else if((str[i]>='a')&&(str[i]<='z')){
a[1]=1;
}else if((str[i]>='0')&&(str[i]<='9')){
a[2]=1;
}else{
a[3]=1;
}
if(i>=2){
string tmp = str.substr(i-2,3);
if(set.empty()||set.find(tmp)==set.end()){
set.insert(str.substr(i-2,3));
}else{
repeat = true;
break;
}
}
}
//计算字符串包含的字符种类总数
for(int i=0;i<4;i++){
if(a[i]==1) count++;
}
if(count<3||repeat==true||len<9) return "NG";
return "OK";
}
int main(){
string str;
while(getline(cin,str)){
cout<<Password_Verification(str)<<endl;
}
return 0;
}