|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + int strongPasswordChecker(string s) { |
| 4 | + const int inf = 0x3f3f3f3f; |
| 5 | + auto n = s.size(); |
| 6 | + const int charset = 2 + 2 + 2; |
| 7 | + vector<vector<vector<vector<vector<int>>>>> f(s.size() + 1, vector<vector<vector<vector<int>>>>(21, vector<vector |
| 8 | + <vector<int>>>(charset, vector<vector<int>>(3, vector<int>(8, inf))))); |
| 9 | + // int f[100][21][charset][3][1 << 3]; |
| 10 | + // memset(f, inf, sizeof(f)); |
| 11 | + f[0][0][0][0][0] = 0; |
| 12 | + for (auto &ch : s) { |
| 13 | + if (isdigit(ch)) { |
| 14 | + ch -= '0'; |
| 15 | + } else if (isupper(ch)) { |
| 16 | + ch -= 'A'; |
| 17 | + ch += 10; |
| 18 | + } else { |
| 19 | + ch -= 'a'; |
| 20 | + ch += 36; |
| 21 | + } |
| 22 | + } |
| 23 | + char last = s[0]; |
| 24 | + for (int i = 0; i < s.size(); i++) { |
| 25 | + bool dif = (i && s[i] != last); |
| 26 | + int lastv = i ? s[i - 1] : 0; |
| 27 | + last = s[i]; |
| 28 | + if (s[i] < 10) { |
| 29 | + s[i] = 0 ^ (dif ^ (lastv & 1)); |
| 30 | + } else if (s[i] < 36) { |
| 31 | + s[i] = 2 ^ (dif ^ (lastv & 1)); |
| 32 | + } else { |
| 33 | + s[i] = 4 ^ (dif ^ (lastv & 1)); |
| 34 | + } |
| 35 | + // cout << int(s[i]) << endl; |
| 36 | + } |
| 37 | + auto type = [] (char x) { |
| 38 | + if (x < 2) { |
| 39 | + return 1 << 0; |
| 40 | + } else if (x < 4) { |
| 41 | + return 1 << 1; |
| 42 | + } else { |
| 43 | + return 1 << 2; |
| 44 | + } |
| 45 | + }; |
| 46 | + for (int len = 0; len < 20; len++) { |
| 47 | + for (int i = 0; i <= n; i++) { |
| 48 | + for (int lastchar = 0; lastchar < charset; lastchar++) { |
| 49 | + for (int repeat = 0; repeat < 3; repeat++) { |
| 50 | + for (int state = 0; state < 8; state++) { |
| 51 | + if (f[i][len][lastchar][repeat][state] >= inf) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + int x = f[i][len][lastchar][repeat][state]; |
| 55 | + // keep |
| 56 | + if (i < n) { |
| 57 | + { |
| 58 | + char ch = s[i]; |
| 59 | + if (repeat < 2 || lastchar != ch) { |
| 60 | + int &y = f[i + 1][len + 1][ch][lastchar == ch ? repeat + 1 : 1][state | type(ch)]; |
| 61 | + y = min(x, y); |
| 62 | + } |
| 63 | + } |
| 64 | + // remove |
| 65 | + { |
| 66 | + int &y = f[i + 1][len][lastchar][repeat][state]; |
| 67 | + y = min(y, x + 1); |
| 68 | + } |
| 69 | + } |
| 70 | + // add or change |
| 71 | + { |
| 72 | + for (int ch = 0; ch < charset; ch++) { |
| 73 | + if (repeat < 2 || lastchar != ch) { |
| 74 | + { |
| 75 | + int &y = f[i][len + 1][ch][lastchar == ch ? repeat + 1 : 1][state | type(ch)]; |
| 76 | + y = min(y, x + 1); |
| 77 | + } |
| 78 | + if (i < n) { |
| 79 | + int &y = f[i + 1][len + 1][ch][lastchar == ch ? repeat + 1 : 1][state | type(ch)] |
| 80 | + ; |
| 81 | + y = min(y, x + 1); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + int ans = inf; |
| 92 | + for (int len = 6; len <= 20; len++) { |
| 93 | + for (int ch = 0; ch < charset; ch++) { |
| 94 | + for (int repeat = 0; repeat < 3; repeat++) { |
| 95 | + ans = min(ans, f[s.size()][len][ch][repeat][7]); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return ans; |
| 100 | + } |
| 101 | +}; |
0 commit comments