結果

問題 No.2454 Former < Latter
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-07-08 09:42:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 872 bytes
コンパイル時間 1,998 ms
コンパイル使用メモリ 200,944 KB
実行使用メモリ 4,824 KB
最終ジャッジ日時 2023-10-01 00:30:08
合計ジャッジ時間 5,049 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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] && i != N-i) ans++;
    }

    cout << ans << endl;
}

int main(){

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

    return 0;
}
0