結果

問題 No.2454 Former < Latter
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-07-07 18:03:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 919 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 200,212 KB
実行使用メモリ 4,908 KB
最終ジャッジ日時 2023-09-30 06:26:54
合計ジャッジ時間 4,838 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 9 ms
4,568 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 8 ms
4,584 KB
testcase_07 AC 9 ms
4,572 KB
testcase_08 AC 9 ms
4,376 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 9 ms
4,820 KB
testcase_12 AC 9 ms
4,624 KB
testcase_13 AC 9 ms
4,660 KB
testcase_14 AC 9 ms
4,648 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 15 ms
4,376 KB
testcase_19 AC 9 ms
4,580 KB
testcase_20 AC 11 ms
4,572 KB
testcase_21 WA -
testcase_22 AC 8 ms
4,376 KB
testcase_23 AC 8 ms
4,380 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