binary search implementation sortings strings *1800

Please click on ads to support us..

Python Code:

import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

t = int(input())
for tidx in range(t):
    s = input().decode().strip()
    l = len(s)
    l1 = [0]*26
    l2 = [-1]*26
    u = 0
    ans2 = ""
    for i in range(l-1, -1, -1):
        j = s[i]
        q = ord(j)-ord('a')
        if l1[q] == 0:
            u += 1
            l2[q] = len(ans2)
            ans2 = j+ans2
        l1[q] += 1
    ls = sum([l1[q]//(u-l2[q]) for q in range(26) if l2[q]>=0])
    ans1 = s[:ls]
    check = ans1
    checksum = ans1
    for j in ans2:
        check = "".join([x for x in check if x != j])
        checksum += check
    if checksum != s:
        print(-1)
    else: print(ans1, ans2)
        

C++ Code:

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "./debug.h"
#else
#define debug(...)
#endif
#define int long long

using namespace std;

const int inf = 1e18;
const int N = 3e5 + 5;
const int mod = 998244353;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    map<char, int> m;
    map<char, int> cnt;
    for (int i = 0; i < n; i++) {
        m[s[i]] = i;
        cnt[s[i]]++;
    }
    string t = s;
    sort(t.begin(), t.end(), [&](auto x, auto y) {
        return m[x] < m[y];
    });
    vector<char> order;
    for (char c : t) {
        if (order.empty() || order.back() != c) {
            order.push_back(c);
        }
    }
    auto get = [&](char c) {
        for (int i = 0; i < order.size(); i++) {
            if (order[i] == c)
                return i + 1;
        }
        return 0ll;
    };
    map<char, int> temp = cnt;
    for (auto& x : temp) {
        int id = get(x.first);
        x.second /= id;
    }
    string res;
    for (char c : s) {
        if (--temp[c] >= 0)
            res.push_back(c);
    }

    auto encrypt = [&]() {
        string cur, here = res;
        for (auto p : order) {
            cur += here;
            string nxt;
            for (char c : here) {
                if (c != p) {
                    nxt.push_back(c);
                }
            }
            here = nxt;
        }
        return cur;
    };
    string encrypted = encrypt();
    if (encrypted != s) {
        cout << "-1\n";
        return;
    }
    cout << res << " ";
    for (char c : order) {
        cout << c;
    }
    cout << "\n";
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.out", "r", stdin);
    freopen("output.out", "w", stdout);
    freopen("error.out", "w", stderr);
#endif
    int T = 1;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        // cout << "Case #" << t << ": ";
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
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