密碼驗證合格程序

題目鏈接

#include <iostream>
#include <cstring>
using namespace std;

bool isThree(string s) {
    int ans[4] = {0, 0, 0, 0};
    for(int i = 0; i < s.length(); i++) {
        if(s[i] >= 'a' && s[i] <= 'z') {
            ans[0] = 1;
        } else if(s[i] >= 'A' && s[i] <= 'Z') {
            ans[1] = 1;
        } else if(s[i] >= '0' && s[i] <= '9') {
            ans[2] = 1;
        } else {
            ans[3] = 1;
        }
    }
    int n = ans[0] + ans[1] + ans[2] + ans[3];
    if(n >= 3) return true;
    return false;
}

//是否沒有重複子串
//沒有返回 true
//有返回 false
bool isNotRepeat(string str) {
    bool flag = true;
    for(int i = 0; i <= str.size() - 6; i++)
        for(int j = i + 3; j < str.size(); j++)
            if(str[i] == str[j] && str[i + 1] == str[j + 1] && str[i + 2] == str[j + 2]) {
				flag = false;
				break;
            } 
/*     
    for(int i = 0; i < s.length() - 4; i++) {
	    char buf[4] = {'0', '0', '0', '\0'};
	    buf[0] = s[i];
		buf[1] = s[i + 1];
		buf[2] = s[i + 2];
		string tmp = buf;
		cout << "當前檢測的子串: " << tmp << endl;		
		if(s.find(tmp, i + 3) != s.npos) {
		    cout << "發現重複子串: " << tmp << endl;
			//cout << "其第一個出現的位置: " << s.find_first_of(tmp) << endl;
		    //cout <<	"其最後一個出現的位置: " << s.find_last_of(tmp) << endl;
			flag = false;
			break;
		}
    }
*/
    return flag;
}

int main() {
    string s;
    while(getline(cin, s)) {
        
        if(s.length() <= 8) {
            cout << "NG" << endl;
            continue;
        }
 
        bool thr = isThree(s);

        bool rep = isNotRepeat(s);	
      
        if(thr == true && rep == true) 
            cout << "OK" << endl;
        else 
            cout << "NG" << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章