1889A - Qingshan Loves Strings 2 - CodeForces Solution


constructive algorithms greedy implementation *1300

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
#define ll long long
using namespace std;


void solve()
{
    int n;
    string s;
    cin>>n;
    cin>>s;
    int count1=0,count0=0;
    for(auto i:s)
    {
        if(i=='1')
        {count1++;}
        else{
            count0++;
        }
    }

    if(count1!=count0)
    {
        cout<<-1<<endl;
    }
    else{
        int l=0,r=s.size()-1;
        string s2="01";
        vector<int> ans;
        int count=0;
        
        while(l<r)
        {
            if(s[l]==s[r])
            {   
                if(s[l]=='1')
                {
                    s.insert(l,s2);
                    ans.push_back(l);
                }
                else{
                    s.insert(r+1,s2);
                    ans.push_back(r+1);
                }
                
                count++;
                
                l=0;
                r=s.size()-1;
            }
            else{
                l++;
                r--;
            }

            
        }
        if(count>300)
        {
            cout<<-1<<endl;
            return;
        }
        cout<<ans.size()<<endl;
        for(auto i:ans)
        {
            cout<<i<<" ";
        }

        cout<<endl;



    }





}

int main()
{
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }


    return 0;
}


Comments

Submit
0 Comments
More Questions

493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour