1. 程式人生 > >華為機測-密碼驗證合格程式

華為機測-密碼驗證合格程式

題目描述

密碼要求:

1.長度超過8位

2.包括大小寫字母.數字.其它符號,以上四種至少三種

3.不能有相同長度超2的子串重複

說明:長度超過2的子串

輸入描述:

一組或多組長度超過2的子符串。每組佔一行

輸出描述:

如果符合要求輸出:OK,否則輸出NG

示例1

輸入

021Abc9000

021Abc9Abc1

021ABC9000

021$bc9000

輸出

OK

NG

NG

OK

程式碼:

int main()
{
	vector<string> ostrs;
	vector<string> istrs;
	string str;
	string OK = "OK";
	string NG = "NG";
	string all;
	cin >> all;
	for(unsigned int ii = 0; ii < all.size(); ii++)
	{
		if(' ' == all[ii])
		{
			istrs.push_back(str);
			str.clear();
		}
		else
			str.push_back(all[ii]);
	}
	for(unsigned int ii = 0; ii < istrs.size(); ii++)
	{
		str = istrs[ii];
		//第一個條件
		if(str.size() < 8)
		{
			ostrs.push_back(NG);
			continue;
		}
		//第二個條件
		char type[4] = {0};

		for(unsigned int i = 0; i < str.size(); i++)
		{
			char c = str[i];
			if('0' <= str[i] && str[i] <= '9')
				type[0] = 1;
			else if('a' <= str[i] && str[i] <= 'z')
				type[1] = 1;
			else if('A' <= str[i] && str[i] <= 'Z')
				type[2] = 1;
			else
				type[3] = 1;
		}
		int sum = 0;
		for(unsigned int j = 0; j < 4; j++)
			sum += type[j];
		if(sum < 3)
		{
			ostrs.push_back(NG);
			continue;
		}
		//第三個條件
		int pos = -1;
		for(unsigned int j = 0; j < str.size(); j++)
		{
			string sstr = str.substr(j,3);//此處3指擇取3個字串
			pos = str.find(sstr,j+3);
			if(pos != string::npos)
				break;
		}
		if(pos == string::npos)
			ostrs.push_back(OK);
		else
			ostrs.push_back(NG);
	}
	for(unsigned int i = 0; i < ostrs.size(); i++)
		cout << ostrs[i] << endl;
	return 0;
}