密码验证合格程序


描述

密码要求:

 

 

 

1.长度超过8位

 

 

 

2.包括大小写字母.数字.其它符号,以上四种至少三种

 

 

 

3.不能有相同长度超2的子串重复

 

 

 

说明:长度超过2的子串


知识点 字符串,数组
运行时间限制 0M
内存限制 0
输入

一组或多组长度超过2的子符串。每组占一行

输出

如果符合要求输出:OK,否则输出NG

 

每行输出对应一组输入的结果;


样例输入 021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
样例输出 OK NG NG OK
二、解法
<pre name="code" class="cpp">#include <iostream>
#include <string>
#include <vector>
//#include <regex>
#include <algorithm>
using namespace std;

//KMP方法中的next表
void getNext(string needle,vector<int> &next)
{
	next[0]=-1;	
	int i=0,k=-1,size=needle.size();
	while(i<size-1)
	{
		if(k==-1 || needle[i]==needle[k])
		{
			if(needle[++i]==needle[++k])
				next[i]=next[k];
			else
				next[i]=k;
		}
		else
		{
			k=next[k];
		}
	}
}
//求最大公共字符长度
int strStr(string haystack, string needle) {
	if(haystack.empty()||needle.empty())
		return 0;

	int i=0,j=0,hSize=haystack.size(),nSize=needle.size(),max=0;
	vector<int> next(nSize,0);
	//获取next表
	getNext(needle,next);
	//求最大公共字符串长度
	while(i<hSize&&j<nSize&&max<3)
	{
		if(j==-1||haystack[i]==needle[j])
		{
			++i,++j;
			max=max<j?j:max;//将每次求出的计算结果的最大值保存在max中
		}
		else
			j=next[j];
	}

	return max;
}
//获取两个字符串的最大长度
int getCommonStrLength(string haystack, string needle)
{
	//若为空,则返回
	if(haystack.empty()||needle.empty())
		return 0;
	int i,size=needle.size(),max=0,tmp;
	string str;
	for(i=0;i<size;i++)
	{
		//避免从中间开始匹配麻烦,保证字符匹配是从头开始匹配的
		str=needle.substr(i);
		tmp=strStr(haystack,str);
		max=max<tmp?tmp:max;
	}

	return max;
}
//判断是否含有大于2的重复字串
bool getRepeat(string str)
{
	vector<string> vec;
	int i,size=str.size();
	for(i=2;i<size-2;i++)
	{
		if(getCommonStrLength(str.substr(0,i),str.substr(i))>2)
			return true;
	}
	return false;
}

int main()
{
	string str;
	int count=0;
	//regex cap("[A-Z]+"),low("[a-z]+"),digital("[0-9]+"),other("[^A-Za-z0-9]");

	while(getline(cin,str))
	{
		//判断是否大于8
		if(str.size()<=8)
		{
			cout<<"NG"<<endl;
			continue;
		}
		//判断是否含有大写、小写、数字和其他字符中任意三种
		int i,size=str.size(),a=0,b=0,c=0,d=0;
		for(i=0;i<size;i++)
		{
			if(str[i]>='a'&&str[i]<='z')
				b=1;
			else if(str[i]>='A'&&str[i]<='Z')
				c=1;
			else if(str[i]>='0'&&str[i]<='9')
				a=1;
			else
				d=1;
		}
		if(a+b+c+d<3)
		{
			cout<<"NG"<<endl;
			continue;
		}
		//判断是否含大于2的字串
		if(getRepeat(str))
			cout<<"NG"<<endl;
		else
			cout<<"OK"<<endl;
	}
	system("pause");
	return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值