結果

問題 No.2454 Former < Latter
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-07-07 15:10:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 919 bytes
コンパイル時間 2,082 ms
コンパイル使用メモリ 199,884 KB
実行使用メモリ 4,916 KB
最終ジャッジ日時 2023-09-28 19:18:12
合計ジャッジ時間 4,442 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 10 ms
4,588 KB
testcase_03 AC 9 ms
4,552 KB
testcase_04 AC 9 ms
4,884 KB
testcase_05 AC 9 ms
4,556 KB
testcase_06 AC 9 ms
4,736 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 10 ms
4,672 KB
testcase_12 WA -
testcase_13 AC 10 ms
4,560 KB
testcase_14 AC 10 ms
4,560 KB
testcase_15 WA -
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 262 ms
4,376 KB
testcase_18 WA -
testcase_19 AC 10 ms
4,732 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 9 ms
4,384 KB
testcase_23 AC 9 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T>
vector<int> z_algorithm(T &S){
    vector<int> Z(S.size());
    Z[0] = S.size();
    int i=1, j=0;
    while(i < S.size()){
        while(i+j < S.size() && S[j] == S[i+j]) j++;
        Z[i] = j;

        if (j == 0){
            i++;
            continue;
        }
        int k=1;
        while(k < j && k + Z[k] < j){
            Z[i+k] = Z[k];
            k++;
        }
        i += k;
        j -= k;
    }

    return Z;
}

void solve(){
    int N, ans=0, k;
    string S;
    cin >> N >> S;
    vector<int> z = z_algorithm(S);

    for (int i=1; i<N; i++){
        if (i < z[i]) ans++;
        else{
            k = z[i];
            if (i+k<N && S[k] < S[i+k]) ans++;
        }
    }

    cout << ans << endl;
}

int main(){

    int T;
    cin >> T;
    while(T){
        T--;
        solve();
    }

    return 0;
}
0