1512C - A-B Palindrome - CodeForces Solution


constructive algorithms implementation strings *1200

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    a, b = list(map(int, input().split()))
    s = list(input())
    n = len(s)

    if ( a%2==1 and b%2==1 ) or ((a%2==1 or b%2==1)and (a+b)%2==0):
        print(-1)
        continue
    
    stop=0
    for i in range(n):
        if s[i] == '?':
            continue
        if s[i] == '1':
            if s[n-1-i] == '0':
                stop=1
                break
        if s[i] == '0':
            if s[n-1-i] == '1':
                stop=1
                break
        
        s[n-i-1] = s[i]

    if stop:
        print(-1)
        continue

    count_a = count_b = 0
    for c in s:
        if c=='1':
            count_b+=1
        elif c=='0':
            count_a+=1
    
    if s[n//2 if n%2==1 else n//2-1]=='?':
        if a%2==1:
            s[n//2 if n%2==1 else n//2-1] = '0'
            count_a +=1
        elif b%2==1:
            s[n//2 if n%2==1 else n//2-1] = '1'
            count_b +=1

    i=0
    j=n-1
    while j>=i:
        if s[i] == '?':
            if count_a < a:
                s[i] = s[n-1-i] = '0'
                count_a += (1 if n-1-i==i else 2)
            elif count_b < b:
                s[i] = s[n-1-i] = '1'
                count_b += (1 if n-1-i==i else 2)

        i+=1
        j-=1

    print(-1 if count_a!=a or count_b!=b else ''.join(s))

C++ Code:

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

int main()
{   

    int t;
    cin>>t;
    
    while(t--){
        
        int a,b;
        cin>>a>>b;
        
        string s;
        cin>>s;
        int n =s.length();
        
        
        bool ans = true;
        int l = 0;
        int r = s.length()-1;
        vector<int> ind;
        
        while(l<r){
            
            if(s[l]==s[r] &&s[l]=='?') {
                ind.push_back(l);
            }
            else if(s[l]=='?'){
                if(s[r]=='1') b-=2;
                else a-=2;
                s[l]=s[r];
            }
            else if(s[r]=='?'){
                if(s[l]=='1') b-=2;
                else a-=2;
                s[r]=s[l];
            }
            else if(s[l]!=s[r]){
                ans =false;
                break;
            }
            else{
                if(s[l]=='1') b-=2;
                else a-=2;
            }
            l++;
            r--;
        }
        
        if(l==r){
            if(s[l]=='0') a--;
            else if(s[l]=='1') b--;
            else{
                ind.push_back(l);
            }
        }
        // cout<<a<<" "<<b<<"\n";
        // for(auto i : ind) cout<<i<<" ";
        // cout<<"\n";
        for(int j=0; j<ind.size();j++){
            int i = ind[j]; 
            if(i!=(n-1-i)){
                if(a>=2){
                    // cout<<"h"<<" "<<i<<"\n";
                    a-=2;
                    s[i]='0';
                    s[n-1-i]='0';
                }
                else if(b>=2){
                    b-=2;
                    s[i]='1';
                    s[n-1-i]='1';
                }
            }
            else{
                if(a>=1){
                    a-=1;
                    s[i]='0';
                }
                else if(b>=1){
                    b-=1;
                    s[i]='1';
                }
            }
        }
        
        if(ans && b==0 && a==0){
            for(int i=0; i<s.length(); i++) cout<<s[i];
            cout<<"\n";
        }
        else cout<<-1<<"\n";
        
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes