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 11:47:28
Task:Dynamic duo
Sender:KnowYourArchitecture
Submission time:2017-05-27 11:47:28
Status:READY
Result:ACCEPTED

Show test data

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:18:14: warning: array subscript has type 'char' [-Wchar-subscripts]
         cnt[c] += 1;
              ^
input/code.cpp:19:17: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(cnt[c] > nn) {
                 ^
input/code.cpp:35:18: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(!cnt[c]) continue;
                  ^
input/code.cpp:39:28: warning: array subscript has type 'char' [-Wchar-subscripts]
         for(int i=0;i<cnt[c];i++) {
                            ^

Code

#include <bits/stdc++.h>

using namespace std;

int cnt[200];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    string s;
    cin >> s;
    
    int n = s.length();
    int nn = n/2;
    
    for(auto c : s) {
        cnt[c] += 1;
        if(cnt[c] > nn) {
            cout << -1 << endl;
            return 0;
        }
    }
    
    //for(char c = 'A'; c <= 'Z'; c++) cout << c << ": " << cnt[c] << endl;
    
    vector<char> A(nn, '*'), B(nn, '*');
    
    bool a = true;
    int idx = 0;
    
    for(char c = 'A'; c <= 'Z'; c++) {
        // find first empty in A/B
        
        if(!cnt[c]) continue;
        
        // cout << c << ": " << idx << ", " << a << endl;
        
        for(int i=0;i<cnt[c];i++) {
            if(a) {
                A[idx] = c;
            } else {
                B[idx] = c;
            }
            idx++;
            a = !a;
            if(idx >= nn) {
                idx = 0;
                a = false;
            }
        }
    }
    
    for(int i=0;i<nn;i++) {
        cout << A[i];
    } cout << endl;

    for(int i=0;i<nn;i++) {
        cout << B[i];
    } cout << endl;
    
    return 0;
}