結果

問題 No.2745 String Swap Battle
ユーザー GOTKAKOGOTKAKO
提出日時 2024-04-20 14:26:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,524 bytes
コンパイル時間 2,998 ms
コンパイル使用メモリ 219,948 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2024-04-20 14:26:46
合計ジャッジ時間 3,875 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,248 KB
testcase_01 AC 5 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 9 ms
5,376 KB
testcase_07 AC 9 ms
5,376 KB
testcase_08 AC 9 ms
5,376 KB
testcase_09 AC 8 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 181 ms
10,240 KB
testcase_13 AC 178 ms
10,240 KB
testcase_14 AC 169 ms
10,116 KB
testcase_15 AC 14 ms
5,376 KB
testcase_16 AC 15 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    vector<string> S(N);
    vector<pair<string,int>> S2(N);
    for(auto &s : S) cin >> s;

    int idx = -1;
    for(auto s : S){
        idx++;
        string t = s;
        sort(t.begin(),t.end());

        int n = s.size();
        vector<vector<int>> C(26);
        for(int i=1; i<n; i++) C.at(s.at(i)-'a').push_back(i);

        if(t == s){
            bool ok = false;
            for(int i=0; i<26; i++){
                if(C.at(i).size() <= 1) continue;
                swap(s.at(C.at(i).at(0)),s.at(C.at(i).at(1)));
                ok = true; break;
            }
            if(!ok) swap(s.at(n-1),s.at(n-2));
            S2.at(idx) = {s,idx};
        }
        else{
            for(int i=0; i<n; i++){
                if(s.at(i) == t.at(i)) continue;
                char c = 'z'+1; int pos = n;
                for(int k=n-1; k>i; k--){
                    if(c > s.at(k)) c = s.at(k),pos = k;
                }
                swap(s.at(i),s.at(pos));
                break;
            }
            S2.at(idx) = {s,idx};
        }
    }
    sort(S2.begin(),S2.end());

    int win = 0; string s0 = S2.at(0).first;
    vector<bool> W(N);
    for(int i=0; i<N; i++){
        if(S2.at(i).first == s0) win++,W.at(S2.at(i).second) = true;
        else break;
    }
    for(auto w : W){
        if(w) cout << N+1-win << endl;
        else cout << 0 << endl;
    }
    
}
0