Advertisement
tepyotin2

Uddered but not herd

Jul 6th, 2025
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int INF = 1e9;
  10.  
  11. void solve() {
  12.     ios_base::sync_with_stdio(false);
  13.     cin.tie(NULL);
  14.  
  15.     string heard_string;
  16.     cin >> heard_string;
  17.  
  18.     map<char, int> char_to_index;
  19.     int unique_char_count = 0;
  20.     for (char c : heard_string) {
  21.         if (char_to_index.find(c) == char_to_index.end()) {
  22.             char_to_index[c] = unique_char_count++;
  23.         }
  24.     }
  25.  
  26.     const int N = unique_char_count;
  27.     if (heard_string.empty()) {
  28.         cout << 0 << "\n";
  29.         return;
  30.     }
  31.     if (N <= 1) {
  32.         cout << 1 << "\n";
  33.         return;
  34.     }
  35.  
  36.     vector<vector<int>> adjacency_counts(N, vector<int>(N, 0));
  37.     for (size_t i = 0; i + 1 < heard_string.length(); ++i) {
  38.         int u = char_to_index[heard_string[i]];
  39.         int v = char_to_index[heard_string[i+1]];
  40.         adjacency_counts[u][v]++;
  41.     }
  42.  
  43.     vector<int> min_hums(1 << N, INF);
  44.     min_hums[0] = 1;
  45.  
  46.     const int low_bits_count = N / 2;
  47.     const int high_bits_count = N - low_bits_count;
  48.     const int low_bits_mask = (1 << low_bits_count) - 1;
  49.  
  50.     vector<vector<int>> precomputed_sums_low(N, vector<int>(1 << low_bits_count, 0));
  51.     vector<vector<int>> precomputed_sums_high(N, vector<int>(1 << high_bits_count, 0));
  52.  
  53.     for (int j = 0; j < N; ++j) {
  54.         for (int mask = 1; mask < (1 << low_bits_count); ++mask) {
  55.             int lsb_idx = __builtin_ctz(mask); // right most bit e.g. 10010 -> 00010
  56.             int prev_mask = mask ^ (1 << lsb_idx);
  57.             precomputed_sums_low[j][mask] = precomputed_sums_low[j][prev_mask] + adjacency_counts[j][lsb_idx];
  58.         }
  59.         for (int mask = 1; mask < (1 << high_bits_count); ++mask) {
  60.             int lsb_idx = __builtin_ctz(mask); // right most bit e.g. 10010 -> 00010
  61.             int prev_mask = mask ^ (1 << lsb_idx);
  62.             precomputed_sums_high[j][mask] = precomputed_sums_high[j][prev_mask] + adjacency_counts[j][low_bits_count + lsb_idx];
  63.         }
  64.     }
  65.  
  66.     for (int mask = 1; mask < (1 << N); ++mask) {
  67.         for (int j = 0; j < N; ++j) {
  68.             if (mask & (1 << j)) {
  69.                 int prev_mask = mask ^ (1 << j);
  70.  
  71.                 int current_total_cost = min_hums[prev_mask];
  72.  
  73.                 int cost_of_adding_j = 0;
  74.                 int high_mask_part = mask >> low_bits_count;
  75.                 cost_of_adding_j += precomputed_sums_low[j][mask & low_bits_mask];
  76.                 cost_of_adding_j += precomputed_sums_high[j][high_mask_part];
  77.  
  78.                 current_total_cost += cost_of_adding_j;
  79.  
  80.                 min_hums[mask] = min(min_hums[mask], current_total_cost);
  81.             }
  82.         }
  83.     }
  84.  
  85.     cout << min_hums[(1 << N) - 1] << "\n";
  86. }
  87.  
  88. int main() {
  89.     solve();
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement