题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <string>
#include <set>
using namespace std;
string fuction(string const& str)
{
set<int> set;
//至少有8个
if(str.length()<8)
{
return "NG";
}
//至少有3种
for(auto i:str)
{
if(i>=48&&i<=57)
{
set.insert(1);
}
if(i>=65&&i<=90)
{
set.insert(2);
}
if(i>=97&&i<=122)
{
set.insert(3);
}
else
set.insert(4);
}
if(set.size()<3)
{
return "NG";
}
//不能有大于2个字符的子串重复
for(int i=0;i<str.length()-5;++i)
{
for(int j=i+3;j<=str.length()-2;++j)
{
if(str[i]==str[j]&&str[i+1]==str[j+1]&&str[i+2]==str[j+2])
{
return "NG";
}
}
}
return "OK";
}
int main()
{
int count=0;
string str;
while(getline(cin, str))
{
cout << fuction(str);
}
return 0;
}
#include <string>
#include <set>
using namespace std;
string fuction(string const& str)
{
set<int> set;
//至少有8个
if(str.length()<8)
{
return "NG";
}
//至少有3种
for(auto i:str)
{
if(i>=48&&i<=57)
{
set.insert(1);
}
if(i>=65&&i<=90)
{
set.insert(2);
}
if(i>=97&&i<=122)
{
set.insert(3);
}
else
set.insert(4);
}
if(set.size()<3)
{
return "NG";
}
//不能有大于2个字符的子串重复
for(int i=0;i<str.length()-5;++i)
{
for(int j=i+3;j<=str.length()-2;++j)
{
if(str[i]==str[j]&&str[i+1]==str[j+1]&&str[i+2]==str[j+2])
{
return "NG";
}
}
}
return "OK";
}
int main()
{
int count=0;
string str;
while(getline(cin, str))
{
cout << fuction(str);
}
return 0;
}