Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- int check_bit(int val, int bit){
- return (val>>bit)&1;
- }
- pair<int, int> push(string & s,vector<int> bits, int i){
- int ans_mask = 0;
- int ans_steps = 2;
- if(i) if(s[bits[i]] == s[bits[i - 1]]) ans_steps--;
- int left_sz = ans_steps;
- int right_sz = 0;
- int left = i - 1 ;
- int right = i;
- while(left >= 0 && s[bits[left]] == s[bits[i]]) left--, left_sz++;
- while(right <(int)bits.size() && s[bits[right]] == s[bits[i]]) right++, right_sz++;
- int real_l = left;
- int real_r = right;
- while(left_sz && right_sz && left_sz + right_sz > 2){
- real_l = left;
- real_r = right;
- if(left == -1 || right == (int)bits.size()) break;
- left_sz = 0;
- right_sz = 0;
- char ch = s[bits[left]];
- while(left >=0 && ch == s[bits[left]]) left--,left_sz++;
- while(right < (int)bits.size() && s[bits[right]] == ch) right++, right_sz++;
- }
- for(int i=0;i<=real_l;i++){
- ans_mask |= (1<<bits[i]);
- }
- for(int i = real_r; i < (int)bits.size();i++){
- ans_mask |= (1<<bits[i]);
- }
- return {ans_mask, ans_steps};
- }
- void setmin(int & val, int b){
- if(b < val) val = b;
- }
- vector<int> depth;
- vector<int> gbits(int val){
- vector<int> ans;
- int cur = 0;
- while(val){
- if(val&1){
- ans.push_back(cur);
- }
- cur++;
- val>>=1;
- }
- return ans;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- depth.assign(1<<14, 2000'000'000);
- string s;
- cin>>s;
- int n = s.length();
- depth[(1<<n) - 1] = 0;
- vector<pair<int,int> > g[1<<n];
- priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;
- pq.push({0, (1<<n) - 1});
- vector<pair<int, char> > ans[1<<n];
- while(pq.size()){
- auto wv = pq.top();
- pq.pop();
- int mask = wv.second;
- if(wv.first > depth[mask]) continue;
- vector<int> bits = gbits(mask);
- for(int i=0;i<(int)bits.size();i++){
- auto cs = push(s, bits, bits[i]);
- if(depth[cs.first] == depth[mask] + cs.second){
- auto possible = ans[mask];
- for(int itr=0; itr<cs.second; itr++){
- possible.push_back({i, s[bits[i]]});
- }
- int lwr = 0;
- for(int i=0;i<(int)possible.size();i++){
- if(possible[i].first > ans[cs.first][i].first){
- break;
- } else if(possible[i].first < ans[cs.first][i].first){
- lwr = 1;
- break;
- }
- }
- if(lwr){
- ans[cs.first] = possible;
- pq.push({depth[cs.first], cs.first});
- }
- } else if(depth[cs.first] > depth[mask] + cs.second){
- depth[cs.first] = depth[mask] + cs.second;
- ans[cs.first] = ans[mask];
- for(int itr = 0; itr < cs.second; itr++){
- ans[cs.first].push_back({i, s[bits[i]]});
- }
- pq.push({depth[cs.first], cs.first});
- }
- }
- }
- cout<<ans[0].size()<<" ";
- for(auto p : ans[0]){
- cout<<p.second<<p.first<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement