HJ 20 Password Verification Qualified Procedure

Ideas:

First, count the case, number, and other occurrences, then determine the length, and finally determine whether there are duplicate substrings.

Statistics of substrings, the complexity is O( N 2);

Title description

Password requirements:

1. More than 8 digits in length

2. Including uppercase and lowercase letters, numbers, other symbols, at least three of the above four

3. There cannot be repeated substrings with the same length greater than 2

Enter description:

One or more sets of strings with a length of more than 2. Each group occupies one line

Output description:

If it meets the requirements, output: OK, otherwise output NG

Example 1

enter

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

Output

OK
NG
NG
OK

Example:

#include<iostream>
using namespace std;
enum {UPPER, LOWER, NUMBER, OTHERS};
int main()
{
    char password[256];
    int type[4];
    while(cin.getline(password, 256)) {
        bool OK = true;
        for(int i = 0; i < 4; i++) type[i] = 0;
        int len = 0;
        char ch = password[len];
        while(ch != '\0') {
            if(isdigit(ch)) {
                type[NUMBER] = 1;
            } else if(isupper(ch)) {
                type[UPPER] = 1;
            } else if(islower(ch)) {
                type[LOWER] = 1;
            } else {
                type[OTHERS] = 1;
            }
            ch = password[++len];
        }
        if(len <= 8) OK = false;
        else {
            int count = 0;
            for(int i = 0; i < 4; i++) {
                count += type[i];
            }
            if(count < 3) OK = false;
            else {
                int match = 0;
                for(int i = 0; i < len; i++) {
                    for(int j = i + 1; j < len; j++) {
                        int tmp = i;
                        match = 0;
                        while(j < len && password[tmp] == password[j]) {
                            tmp++, j++, match++;
                        }
                        if(match > 2) {
                            OK = false;
                            break;
                        }
                    }
                    if(!OK) break;
                }
            }
        }
        cout << (OK ? "OK\n" : "NG\n");
    }
}

 

Guess you like

Origin blog.csdn.net/u012571715/article/details/114966189