松江Ruby会議ミッション / paizaOnlineHackathon6+

paiza.jp

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;

void Solve(int n)   {
    map<string,int> w, left, center;
    string word, str, leftWord = "", centerWord = "", rightWord = "";

    w.clear(); left.clear(); center.clear();
    for (int i=0; i<n; ++i) {
        cin >> word;
        if ( w.find(word) == w.end() )  w[word] = 1;
        else    ++w[word];
    }
    for (map<string,int>::iterator it=w.begin(); it!=w.end(); ++it) {
        str = it->first;
        reverse( str.begin(),str.end() );
        if (it->first == str)    center[str] = it->second;
        else if ( w.find(str) != w.end() )  left[min(it->first,str)] = min(it->second,w[str]);
    }
    for (map<string,int>::iterator it=center.begin(); it!=center.end(); ++it)
        if (it->second >= 2) {
            left[it->first] = it->second/2;
            it->second %= 2;
        }
    for (map<string,int>::iterator it=left.begin(); it!=left.end(); ++it)
        for (int i=0; i<it->second; ++i)
            leftWord += it->first;
    for (map<string,int>::iterator it=center.begin(); it!=center.end(); ++it)
        if (it->second == 1) centerWord += it->first;
    rightWord = leftWord;
    reverse( rightWord.begin(),rightWord.end() );
    cout << leftWord + centerWord + rightWord << endl;
}

int main(void)  {
    int n;

    cin >> n;
    Solve(n);
    return 0;
}

左側と真ん中に分けて登録し右側は左側を逆順にしたものを用い出力。
〜了〜