結果

問題 No.2454 Former < Latter
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-07-07 16:22:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 990 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 199,908 KB
実行使用メモリ 4,628 KB
最終ジャッジ日時 2023-08-28 19:33:22
合計ジャッジ時間 4,111 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 9 ms
4,612 KB
testcase_03 AC 8 ms
4,560 KB
testcase_04 AC 8 ms
4,608 KB
testcase_05 AC 9 ms
4,552 KB
testcase_06 AC 8 ms
4,548 KB
testcase_07 AC 9 ms
4,604 KB
testcase_08 AC 10 ms
4,376 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 9 ms
4,628 KB
testcase_12 AC 9 ms
4,556 KB
testcase_13 AC 9 ms
4,564 KB
testcase_14 AC 9 ms
4,624 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 246 ms
4,380 KB
testcase_18 AC 15 ms
4,376 KB
testcase_19 AC 10 ms
4,608 KB
testcase_20 AC 12 ms
4,560 KB
testcase_21 AC 12 ms
4,380 KB
testcase_22 AC 9 ms
4,376 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 if (i == z[i]){
            if (i < N-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