-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_password.txt
60 lines (57 loc) · 2.26 KB
/
valid_password.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Verify if the given password is valid/invalid;
1. must be 5-12 characters long
2. must contain at least one number and one lowercase character
3. a sequence must not be followed by the same sequence (like 123123qs is invalid, 123qs123 is valid)
*/
#include <iostream>
using namespace std;
int main(){
cout<<"Input your password: "<<endl;
string pw; //pw = password
cin>>pw;
/*check if there are 5-12 characters*/
if (pw.length()>12 || pw.length()<5){
cout<<"Invalid password. length must be 5~12."<<endl;
return -1;
}
/*check if the password contains at least one number and one lowercase character*/
int nnum=0, nlowercase=0; //the number of number and lowercase character
for (int i=0; i<pw.length(); ++i){
if (nnum>=1 && nlowercase>=1)
break;
if (pw[i]>='0'&&pw[i]<='9')
nnum++;
if (pw[i]>='a' && pw[i]<='z')
nlowercase++;
}
if (nnum<1 || nlowercase<1){
cout<<"Invalid password. must contain one number and one lowercase character."<<endl;
return -1;
}
/*check if there are the same sequence following another
Note: i means the starting point, j means the length of the subsequence
*/
for (int i=0; i<pw.length()-1; ++i){
int maxSubseqLength = (pw.length()-i)/2;
for (int j=1; j<maxSubseqLength; ++j){
int start1=i, start2 = i+j, k=0;//k is used to loop through the subsequence.
bool flag=1;
while(k<maxSubseqLength){
if (pw[start1]==pw[start2]){
flag=false;
break;
}
k++;
}
if (flag==false){
cout<<"REPEATED SUBSEQUENCE, Invalid password."<<endl;
return -1;
}
}
}
cout<<"valid password!"<<endl;
return 0;
}
REMARK:
1 IDEA: The idea is that since the 2 subsequence should be adjacent. So the outer loop(i) walk through the start point of the first subsequence.Then, we get the longest length of the subsequence maxSubseqLength. Next in the inner loop(j), we walk through(using j) all the possible length of the subsequence corresponding to the start point i. Then, we check by walking through the subsequence using k.