1689A - Lex String - CodeForces Solution


brute force greedy implementation sortings two pointers *800

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.readline

from math import ceil, floor, sqrt, log2
from heapq import heappush, heappop
from collections import deque
from functools import lru_cache
from bisect import bisect_left, bisect_right

def rl(t = int):
    return list(map(t, input().split()))

T = int(input())

for t in range(1, T + 1):
    n, m, k = rl()
    a = input().strip()
    b = input().strip()

    sa = sorted(a)
    sb = sorted(b)

    ret = []
    i = j = 0
    row = [0, 0]
    while i < n and j < m:
        if (sb[j] < sa[i] or row[0] == k) and row[1] < k:
            ret.append(sb[j])
            row = [0, row[1] + 1]
            j += 1
        else:
            ret.append(sa[i])
            row = [row[0] + 1, 0]
            i += 1

    print(''.join(ret))

C++ Code:

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

int n, m, k;
string a, b, s;
int c1[26], c2[26];

void solution() {
    cin >> n >> m >> k >> a >> b;
    for(int i = 0;i < n;i++) {
        c1[ a[i] - 97 ]++;
    }
    for(int j = 0;j < m;j++) {
        c2[ b[j] - 97 ]++;
    }

//    for(int i =0 ;i < 26;i++) cout << c1[i] << ' '; cout << '\n';
//    for(int i =0 ;i < 26;i++) cout << c2[i] << ' '; cout << '\n';

    int I = 0, J = 0;
    int cnt = 0, last = -1;
    while(!c1[I]) I++;
    while(!c2[J]) J++;

    while(n && m) {

        if(last == 1) {
            if(I < J && cnt != k) {
                s += I + 97;
                c1[I]--;
                while(!c1[I] && I < 26) I++;
                cnt++;
                n--;
            }
            else {
                s += J + 97;
                c2[J]--;
                while(!c2[J] && J < 26) J++;
                cnt = 1;
                last = 2;
                m--;
            }
        }
        else if(last == 2) {
            if(J < I && cnt != k){
                s += J + 97;
                c2[J]--;
                while(!c2[J] && J < 26) J++;
                cnt++;
                m--;
            }
            else {
                s += I + 97;
                c1[I]--;
                while(!c1[I] && I < 26) I++;
                cnt = 1;
                last = 1;
                n--;
            }
        }
        else {
            if(I < J) {
                s += I + 97;
                c1[I]--;
                while(!c1[I] && I < 26) I++;
                cnt = 1;
                last = 1;
                n--;
            }
            else {
                s += J + 97;
                c2[J]--;
                while(!c2[J] && J < 26) J++;
                cnt = 1;
                last = 2;
                m--;
            }
        }
    }

    cout << s << '\n';
//    cout << "\n\n";
}

int main() {
    //freopen("samples.txt", "r", stdin);
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int t = 1;
    cin >> t;
    while(t--) {
        solution();
        for(int i = 0;i < 26;i++) {
            c1[i] = 0;
            c2[i] = 0;
        }
        s.clear();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome