1277D - Let's Play the Words - CodeForces Solution


data structures hashing implementation math *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
 
int main()
{
    ll t;
    cin>>t;
    while(t--){
        vector<ll>v,v1;
        ll n;
        cin>>n;
        string s[n];
        map<string,ll>mpp;
        ll a=0,b=0,c=0,d=0;
        for(ll i=0;i<n;i++){
            cin>>s[i];
            ll x=s[i][0]-'0';
            ll y=s[i][s[i].length()-1]-'0';
            mpp[s[i]]++;
            
            if(x==0&&y==0){
                a++;
            }
            else if(x==0&&y==1){
                b++;
               
            }
            else if(x==1&&y==0){
                c++;
               
            }
            else{
                d++;
            }
        }
        for(ll i=0;i<n;i++){
            ll x=s[i][0]-'0';
            ll y=s[i][s[i].length()-1]-'0';
          if(x==0&&y==1){
              string p=s[i];
              reverse(s[i].begin(),s[i].end());
              if(mpp[s[i]]==0){
                  v.push_back(i+1);
              }
              s[i]=p;
          }
          else if(x==1&&y==0){
              string p=s[i];
              reverse(s[i].begin(),s[i].end());
              if(mpp[s[i]]==0){
                  v1.push_back(i+1);
              }
              s[i]=p;
          }
        }
        if(a!=0&&d!=0&&(b+c)==0){
            cout<<-1<<endl;
        }
        else{
            vector<ll>ans;
            ll flg=0;
            if(a==0&&b==0&&c==0){
                
            }
            else if(b==0&&c==0&&d==0){
                
           }
            else if(a==0&&d!=0){
              
                if(b>c){
                    ll p=b-c;
                    ll k=p/2;
                   
                    if(v.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v[i]);
                    }
                    }
                }
               else{
                   ll p=c-b;
                   ll k=p/2;
                   k=max(0LL,k);
                   if(v1.size()<k){
                       flg=1;
                   }
                   else{
                   for(ll i=0;i<k;i++){
                       ans.push_back(v1[i]);
                   }}
                   
               }
                  
                      
               
            }
            else if(a!=0&&d==0){
                
                if(c>b){
                    ll p=c-b;
                    ll k=p/2;
                    
                    if(v1.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v1[i]);
                    }
                    }
                }
               else{
                   ll p=b-c;
                   ll k=p/2;
                   k=max(k,0LL);
                   if(v.size()<k){
                       flg=1;
                   }
                   else{
                       
                   for(ll i=0;i<k;i++){
                       ans.push_back(v[i]);
                   }
                   }
                   
               }
                  
            }
            else if(a==0&&d==0){
                ll x=abs(b-c);
                ll p=x/2;
                if(b>c){
                    if(v.size()<p){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<p;i++){
                        ans.push_back(v[i]);
                    }
                    }
                }
                else{
                    if(v1.size()<p){
                        flg=1;
                    }
                    else{
                      for(ll i=0;i<p;i++){
                        ans.push_back(v1[i]);
                    }
                    }
                }
            }
            else{
                
                if(b==c){
                    
                }
                else if(b>c){
                    ll g=b-c;
                    ll k=g/2;
                    if(v.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v[i]);
                    }
                    }
                }
                else{
                    ll g=c-b;
                    ll k=g/2;
                    if(v1.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v1[i]);
                    }
                    }
                }
                
            }
            if(flg==0){
            cout<<ans.size()<<endl;
            for(ll i=0;i<ans.size();i++){
                cout<<ans[i]<<" ";
            }
            cout<<endl;}
            else{
                cout<<-1<<endl;
            }
        }
        
        
        
        
    }
}
 


Comments

Submit
0 Comments
More Questions

1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum