1559B - Mocha and Red and Blue - CodeForces Solution


dp greedy *900

Please click on ads to support us..

Python Code:

def find_first_letter(input_str):
    for i in range(len(input_str)):
        if input_str[i] == 'R':
            return 'R' if i & 1 == 0 else 'B'
        if input_str[i] == 'B':
            return 'B' if i & 1 == 0 else 'R'
 
    return 'B'
 
t = int(input())
 
for iter in range(t):
    n = int(input())
    input_str = input()
    result_str = ''
    if input_str[0] == '?':
        if n > 1:
            result_str += find_first_letter(input_str)
            input_str = result_str + input_str[1:]
        else:
            print('B')
            continue
    else:
        result_str += input_str[0]
 
    for i in range(1, n):
        if input_str[i] in ['R', 'B']:
            result_str += input_str[i]
            continue
 
        if result_str[i - 1] == 'B':
            result_str += 'R'
        elif result_str[i - 1] == 'R':
            result_str += 'B'
 
    print(result_str)
 

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define py cout<<"YES"<<endl
#define pn cout<<"NO"<<endl
#define e cout<<"=\n"
#define g cout<<">\n"
#define l cout<<"<\n"
#define Pans cout<<"ans=="<<" ";
bool is_lleger(float k) {return floor(k) == k;}

int main()
{
    ll t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s;
        cin>>s;
        int i=0;
        while(s[i]=='?'){
            i++;
        }

        if(i==n){
            char c='B';
            for(int i=0;i<n;i++){
                s[i]=c;
                if(c=='R')c='B';
                else c='R';
            }
        }
        else{
            for(int j=i+1;j<n;j++){
                if(s[j]=='?'){
                    if(s[j-1]=='R') s[j]='B';
                    else if(s[j-1]=='B') s[j]='R';
                }
            }
            for(i=i-1;i>=0;i--){
                if(s[i]=='?'){
                    if(s[i+1]=='R') s[i]='B';
                    else if(s[i+1]=='B') s[i]='R';
                }
            }
        }

      //   Pans;
        cout<<s<<endl;
    }
return 0;
}


Comments

Submit
0 Comments
More Questions

AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible