cf1277D-- thinking greedy + string

Always want complicated subject, hey

/*
All put together 0x0, 1x1 put together
Consider 0x1 or 1x0,
If there are 0x0 and 1x1, 0x1 but no or 1x0, it is not feasible
All 0x0,1x1 are connected at both ends with a 0x1, 0x1 and 1x0 consider so long as you can
If there is a corresponding string of playing, then the string can not be turned over, and vice versa is possible,
    I.e. from 0x1-> 1x0, or 1x0-> 0x1 

*/
#include<bits/stdc++.h>
using namespace std;
#define N 200005

int n;
set<string>se[2];
map<string,int>mp;
void init(){
    se[0].clear();se[1].clear();
    mp.clear();
}

int main(){
    int t;cin>>t;while(t--){
        init();
        
        cin>>n;
        int cnt00=0,cnt11=0;
        for(int i=1;i<=n;i++){
            string s;
            cin>>s;
            if(s[0]=='0' && s[s.size()-1]=='0')    
                cnt00++;
            else if(s[0]=='1' && s[s.size()-1]=='1')
                cnt11++;
            else if(s[0]=='0' && s[s.size()-1]=='1')
                se[0].insert(s);
            else se[1].insert(s);
            Mp [S] = I;
        }
        
        if(cnt00 && cnt11 && !se[0].size() && !se[1].size()){
            puts("-1");continue;
        }
        
        
        int size0=se[0].size(),size1=se[1].size();
        int tot=se[0].size()+se[1].size();
        vector<int>ans;
        if(se[0].size()>se[1].size()){
            for(auto ss:se[0]){
                if(size1>=tot/2)break;
                string tmp=ss;
                reverse(tmp.begin(),tmp.end());
                if(se[1].find(tmp)!=se[1].end())continue;
                else {
                    size0--;size1++;
                    ans.push_back(mp[ss]);
                }
            }
        }
        else {
            for(auto ss:se[1]){
                if(size0>=tot/2)break;
                string tmp=ss;
                reverse(tmp.begin(),tmp.end());
                if(se[0].find(tmp)!=se[0].end())continue;
                else {
                    size1--;size0++;
                    ans.push_back(mp[ss]);
                }
            }
        }
        
        if(abs(size0-size1)<=1){
            cout<<ans.size()<<'\n';
            for(auto x:ans)cout<<x<<" ";
            puts("");
        }
        else puts("-1");
    }
    
}
 

Guess you like

Origin www.cnblogs.com/zsben991126/p/12110193.html