Advertisement
999ms

707 acmp. v2

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