题目
描述: 密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
题目类别: 字符串,数组
难度: 中级
运行时间限制: 10Sec
内存限制: 128MByte
阶段: 入职前练习
输入:
一组或多组长度超过2的子符串。每组占一行
输出:
如果符合要求输出:OK,否则输出NG
每行输出对应一组输入的结果;
样例输入: 021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
样例输出: OK
NG
NG
OK
代码
/*---------------------------------------
* 日期:2015-07-01
* 作者:SJF0115
* 题目:密码验证合格程序
* 来源:华为上机
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
bool CheckPasswd(string str){
int size = str.size();
if(size <= 8){
return false;
}//if
int isNum = 0,isLower = 0,isUpper = 0,isOther = 0;
int total = 0;
for(int i = 0;i < size;++i){
// 字母
if(str[i] >= 'a' && str[i] <= 'z'){
isLower = 1;
}//if
else if(str[i] >= 'A' && str[i] <= 'Z'){
isUpper = 1;
}//else
else if(str[i] >= '0' && str[i] <= '9'){
isNum = 1;
}//else
else{
isOther = 1;
}//else
total = isNum + isLower + isUpper + isOther;
if(total >= 3){
break;
}//if
}//for
//包括大小写字母.数字.其它符号,以上四种至少三种
if(total < 3){
return false;
}//if
// 暴力
int s1,s2,count = 0;
for(int i = 0;i < size;++i){
for(int j = i + 1;j < size;++j){
s1 = i,s2 = j;
while(s1 < size && s2 < size && str[s1] == str[s2]){
++count;
++s1;
++s2;
if(count >= 3){
return false;
}//if
}//if
count = 0;
}//for
}//for
return true;
}
int main(){
string str;
bool result;
while(getline(cin,str)){
result = CheckPasswd(str);
if(result){
cout<<"OK"<<endl;
}//if
else{
cout<<"NG"<<endl;
}//else
}//while
return 0;
}
查找相同的子串时使用暴力法待改进。。。。。。。