Advertisement
Vince14

Uddered But Not Herd

Mar 19th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <iomanip>
  14. #include <regex>
  15. #include <numeric>
  16. using namespace std;
  17. #define pii pair<int , int>
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
  19. const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  20. const int MAX = 100005;
  21.  
  22. string s;
  23. int arr[26];
  24. int used = 0;
  25. int consecutive[20][20];
  26. int dp[(1 << 20)];
  27.  
  28. int main() {
  29.     //freopen("talent.in", "r", stdin);
  30.     //freopen("talent.out", "w", stdout);
  31.     FAST;
  32.     cin >> s;
  33.     for(int i = 0; i < 26; i++){
  34.         arr[i] = -1;
  35.     }
  36.     for(int i = 0; i < s.size(); i++) {
  37.         if(i == 0){
  38.             arr[s[0] - 'a'] = used;
  39.             used++;
  40.         }
  41.         int nxt = i + 1;
  42.         if (nxt >= s.size()) {
  43.             continue;
  44.         }
  45.         if (arr[s[nxt] - 'a'] == -1) {
  46.             arr[s[nxt] - 'a'] = used;
  47.             used++;
  48.         }
  49.         consecutive[arr[s[i] - 'a']][arr[s[nxt] - 'a']]++;
  50.     }
  51.     memset(dp, 2e9, sizeof dp);
  52.     dp[0] = 0;
  53.     for(int state = 1; state < (1 << 20); state++){
  54.         dp[state] = 2e9;
  55.         for(int cur_char = 0; cur_char < 20; cur_char++){
  56.             if(!(state & (1 << cur_char))){
  57.                 continue;
  58.             }
  59.             int val = dp[state ^ (1 << cur_char)];
  60.             for(int added_char = 0; added_char < 20; added_char++){
  61.                 if(!(state & (1 << added_char))){
  62.                     continue;
  63.                 }
  64.                 val += consecutive[cur_char][added_char];
  65.             }
  66.             dp[state] = min(dp[state], val);
  67.         }
  68.     }
  69.     cout << dp[(1 << 20) - 1] + 1<< "\n";
  70.  
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement