Advertisement
999ms

acmp Zuma 707.

Apr 13th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. vector<int> dp;
  6.  
  7. int check_bit(int val, int bit){
  8.   return (val>>bit)&1;
  9.  
  10. }
  11. pair<int, int> push(string & s,vector<int> bits, int i){
  12.   int ans_mask = 0;
  13.   int ans_steps = 2;
  14.   if(i) if(s[bits[i]] == s[bits[i - 1]]) ans_steps--;
  15.   int left_sz = ans_steps;
  16.   int right_sz = 0;
  17.   int left = i - 1 ;
  18.   int right = i;
  19.   while(left >= 0 && s[bits[left]] == s[bits[i]]) left--, left_sz++;
  20.   while(right <(int)bits.size() && s[bits[right]] == s[bits[i]]) right++, right_sz++;
  21.   int real_l = left;
  22.   int real_r = right;
  23.   while(left_sz && right_sz && left_sz + right_sz > 2){
  24.     real_l = left;
  25.     real_r = right;
  26.     if(left == -1 || right == (int)bits.size()) break;
  27.     left_sz = 0;
  28.     right_sz = 0;
  29.     char ch = s[bits[left]];
  30.     while(left >=0 && ch == s[bits[left]]) left--,left_sz++;
  31.     while(right < (int)bits.size() && s[bits[right]] == ch) right++, right_sz++;
  32.   }
  33.   for(int i=0;i<=real_l;i++){
  34.     ans_mask |= (1<<bits[i]);
  35.   }
  36.   for(int i = real_r; i < (int)bits.size();i++){
  37.     ans_mask |= (1<<bits[i]);
  38.   }
  39.   return {ans_mask, ans_steps};
  40. }
  41.  
  42. void setmin(int & val, int b){
  43.   if(b < val) val = b;
  44. }
  45.  
  46. struct edge{
  47.   int t, st, i;
  48.   edge(int t, int st, int i){
  49.     this->t = t;
  50.     this->st = st;
  51.     this->i = i;
  52.   }
  53. };
  54.  
  55. int main() {
  56.   ios_base::sync_with_stdio(false);
  57.   cin.tie(nullptr);
  58.   dp.assign(1<<14, 2000'000'000);
  59.   string s;
  60.   cin>>s;
  61.   int n = s.length();
  62.   dp[(1<<n) - 1] = 0;
  63.   for(int i=0;i<(1<<n);i++){
  64.     from[i] = i;
  65.   }
  66.   vector<pair<int,int> > masks;
  67.   for(int i=0;i<(1<<(int)(s.length()));i++){
  68.     masks.push_back({__builtin_popcount(i), i});
  69.   }
  70.  
  71.   vector<edge> g[1<<n];
  72.   sort(masks.begin(), masks.end(), greater<>());
  73.   for(auto p : masks){
  74.     int mask = p.second;
  75.     vector<int> bits;
  76.     for(int i=0;i<n;i++){
  77.       if(check_bit(mask, i))
  78.         bits.push_back(i);
  79.     }
  80.     if((int) bits.size() == 1){
  81.       dp[(1<<n)-1] = min(dp[(1<<n) - 1], dp[mask] + 2);
  82.       continue;
  83.     }
  84.     for(int i = 0; i < (int)bits.size(); i++){
  85.       auto cs = push(s, bits, i);
  86.       if(dp[cs.first] > dp[mask] + cs.second){
  87.         dp[cs.first] = dp[mask] + cs.second;
  88.         g[mask].push_back(edge(cs.first, cs.second, bits[i]));
  89.       }
  90.     }
  91.   }
  92.   cout<<dp[0]<<" ";
  93.  
  94.  
  95.  
  96.   return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement