Code Submission Evaluation System Login

CSES - HIIT Open 2017

HIIT Open 2017

Contest start:2017-05-27 11:00:00
Contest end:2017-05-27 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard


History
2017-05-27 12:15:34
2017-05-27 12:04:14
Task:Dynamic duo
Sender:kyy-kol
Submission time:2017-05-27 12:15:34
Status:READY
Result:ACCEPTED

Show test data

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < s.size()/2; ++i) {
                                  ^

Code

#include <iostream>
#include <map>

using namespace std;
int cc[26];
multimap<int, int> ca;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin >> s;
    if (s.size()%2) {
        cout << -1 << endl;
        return 0;
    }
    string a, b;
    for (char c : s) {
        cc[c-'A']++;
    }
    for (int ci = 0; ci < 26; ++ci) {
        if (cc[ci]) ca.insert({cc[ci], ci});
    }
    for (int i = 0; i < s.size()/2; ++i) {
        if (ca.size()<2) {
            cout << -1 << endl;
            return 0;
        }
        int q = (--ca.end())->second; ca.erase(--ca.end());
        int w = (--ca.end())->second; ca.erase(--ca.end());
        a.push_back(q+'A');
        b.push_back(w+'A');
        cc[w]--; cc[q]--;
        if (cc[w]) ca.insert({cc[w], w});
        if (cc[q]) ca.insert({cc[q], q});
    }
    cout << a << endl;
    cout << b << endl;
}