結果

問題 No.470 Inverse S+T Problem
コンテスト
ユーザー vjudge1
提出日時 2026-06-19 10:27:53
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,406 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,354 ms
コンパイル使用メモリ 201,968 KB
実行使用メモリ 10,840 KB
最終ジャッジ日時 2026-06-19 10:27:57
合計ジャッジ時間 4,131 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 18 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
string ans[N];
struct node {
    string s;
    int idx, cnt;
} a[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    map<char, int> freq;
    for (int i = 0; i < n; i++) {
        cin >> a[i].s;
        a[i].idx = i;
        freq[a[i].s[1]]++;
    }
    for (int i = 0; i < n; i++) {
        a[i].cnt = freq[a[i].s[1]];
    }
    sort(a, a + n, [](const node &x, const node &y) {
        return x.cnt > y.cnt;
    });
    set<string> used;
    bool impossible = false;
    for (int i = 0; i < n; i++) {
        string s = a[i].s;
        string s1 = s.substr(0, 1);
        string s2 = s.substr(1, 2);
        if (used.find(s1) == used.end() && used.find(s2) == used.end()) {
            used.insert(s1);
            used.insert(s2);
            ans[a[i].idx] = s1 + " " + s2;
            continue;
        }
        s1 = s.substr(0, 2);
        s2 = s.substr(2, 1);
        if (used.find(s1) == used.end() && used.find(s2) == used.end()) {
            used.insert(s1);
            used.insert(s2);
            ans[a[i].idx] = s1 + " " + s2;
            continue;
        }
        impossible = true;
        break;
    }
    if (impossible) {
        cout << "Impossible\n";
    } else {
        for (int i = 0; i < n; i++) {
            cout << ans[i] << '\n';
        }
    }
    return 0;
}
0