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:37:15
Task:Dynamic duo
Sender:Kanadan virallinen maajoukkue
Submission time:2017-05-27 11:37:15
Status:READY
Result:ACCEPTED

Show test data

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:11:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<s.length(); i++)
                             ^
input/code.cpp:12:17: warning: array subscript has type 'char' [-Wchar-subscripts]
         cnt[s[i]]++;
                 ^
input/code.cpp:18:17: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(cnt[c]>0)
                 ^
input/code.cpp:19:36: warning: array subscript has type 'char' [-Wchar-subscripts]
             pq.push(make_pair(cnt[c],c));
                                    ^

Code

#include<iostream>
#include<queue>

using namespace std;

int cnt[256];

int main(){
    string s;
    cin>>s;
    for(int i=0; i<s.length(); i++)
        cnt[s[i]]++;
        
    string a,b;
    priority_queue<pair<int, char>> pq;
    
    for(char c='A'; c<='Z'; c++)
        if(cnt[c]>0)
            pq.push(make_pair(cnt[c],c));
    
    while(pq.size()){
        if(pq.size()==1){
            cout<<-1<<endl;
            return 0;
        }
        
        pair<int,char> p1,p2;
        p1=pq.top();
        pq.pop();
        p2=pq.top();
        pq.pop();
        
        a+=p1.second;
        b+=p2.second;
        
        p1.first--;
        p2.first--;
        
        if(p1.first>0)
            pq.push(p1);
        if(p2.first>0)
            pq.push(p2);
    }
    
    
    cout<<a<<endl;
    cout<<b<<endl;
    return 0;
}