Advertisement
SorahISA

B. 密碼強度評估 (passcheck)

Oct 5th, 2022
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isextra(char c) {return !isalnum(c);}
  5.  
  6. int main() {
  7.     // ios_base::sync_with_stdio(0), cin.tie(0);
  8.    
  9.     int N; cin >> N, cin.ignore();
  10.    
  11.     int cnt_0 = 0;
  12.     vector<pair<int, string>> pwd(N, {0, ""});
  13.     for (auto &[score, pass] : pwd) {
  14.         getline(cin, pass);
  15.        
  16.         // cerr << "\"" << pass << "\"" << "\n";
  17.        
  18.         /// rule 1: |pass| >= 4 and no space ///
  19.         if ((int)pass.size() < 4 or pass.find(' ') != string::npos) {++cnt_0; continue;}
  20.        
  21.         /// rule 2: score += 2 * max(|pass| - 8, 0) + 10 ///
  22.         score += 2 * max((int)pass.size() - 8, 0) + 10;
  23.        
  24.         // cerr << "r2: " << score << "\n";
  25.        
  26.         int cnt_upper = 0, cnt_lower = 0, cnt_digit = 0, cnt_extra = 0;
  27.         for (char c : pass) cnt_upper += isupper(c), cnt_lower += islower(c), cnt_digit += isdigit(c), cnt_extra += isextra(c);
  28.        
  29.         /// rule 3: score -= 6 if cnt_extra < 3 ///
  30.         if (cnt_extra < 3) score -= 6;
  31.  
  32.         /// rule 4: type = {upper, lower, digit}, score += 2 with each type found ///
  33.         score += 2 * (!!cnt_upper + !!cnt_lower + !!cnt_digit);
  34.  
  35.         /// rule 5: score += 10 * floor(cnt_extra / 5) ///
  36.         score += 10 * (cnt_extra / 5);
  37.        
  38.         // cerr << "r5: " << score << "\n";
  39.        
  40.         /// rule 6: each alternate {alpha, digit, extra} let score += 2 ///
  41.         char flag_rule_6 = pass[0];
  42.         for (char &c : pass) {
  43.             if (
  44.                     (isalpha(c) and !isalpha(flag_rule_6))
  45.                 or  (isdigit(c) and !isdigit(flag_rule_6))
  46.                 or  (isextra(c) and !isextra(flag_rule_6))
  47.             ) score += 2;
  48.             flag_rule_6 = c;
  49.         }
  50.        
  51.         // cerr << "r6: " << score << "\n";
  52.        
  53.     }
  54.     sort(pwd.rbegin(), pwd.rend());
  55.    
  56.     cout << cnt_0 << "\n";
  57.     for (int i = 0; i < N-cnt_0; ++i) {
  58.         cout << pwd[i].first << " " << pwd[i].second << "\n";
  59.     }
  60.    
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement