結果

問題 No.1848 Long Prefixes
ユーザー 👑 NachiaNachia
提出日時 2022-02-18 21:56:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,422 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 9,016 KB
最終ジャッジ日時 2023-09-11 19:04:22
合計ジャッジ時間 3,639 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
8,948 KB
testcase_01 AC 20 ms
8,980 KB
testcase_02 AC 30 ms
8,916 KB
testcase_03 AC 30 ms
8,908 KB
testcase_04 AC 19 ms
9,016 KB
testcase_05 AC 32 ms
8,800 KB
testcase_06 AC 27 ms
8,740 KB
testcase_07 AC 18 ms
8,688 KB
testcase_08 AC 27 ms
8,640 KB
testcase_09 AC 28 ms
8,648 KB
testcase_10 AC 23 ms
8,924 KB
testcase_11 AC 30 ms
8,912 KB
testcase_12 AC 28 ms
8,608 KB
testcase_13 AC 18 ms
8,596 KB
testcase_14 AC 29 ms
8,940 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,388 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 2 ms
4,384 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 13 ms
5,412 KB
testcase_34 AC 17 ms
7,364 KB
testcase_35 AC 7 ms
4,380 KB
testcase_36 AC 12 ms
5,412 KB
testcase_37 AC 14 ms
6,532 KB
testcase_38 AC 16 ms
6,204 KB
testcase_39 AC 12 ms
5,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
using namespace std;


using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned int;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

#include <vector>
#include <string>

template<class T>
vector<int> ZAlgorithm(const T& S) {
    int n = S.size();
    vector<int> res(n, 0);
    res[0] = n;
    int pbegin = 1, pend = 1;
    for (int i = 1; i < n; i++) {
        if (i < pend) {
            res[i] = res[i - pbegin];
            if (pend > i + res[i]) continue;
            res[i] = pend - i;
            pbegin = i;
        }
        else {
            pbegin = pend = i;
        }
        while (true) {
            if (pend == n) break;
            if (S[pend] != S[res[i]]) break;
            res[i]++;
            pend++;
        }
    }
    return res;
}


using modint = atcoder::static_modint<1'000'000'007>;


int main() {
    int N; cin >> N;
    vector<int> A(N);
    rep(i,N) cin >> A[i];
    string S; cin >> S;

    if(N == 1){
        modint ans = modint(A[0]) * modint(A[0]+1) / 2;
        cout << ans.val() << endl;
        return 0;
    }

    string S1 = S.substr(1);
    vector<pair<char, int>> SA1(N-1);
    rep(i,N-1) SA1[i] = make_pair(S[i+1],A[i+1]);

    auto ZS1 = ZAlgorithm(S1);
    auto ZSA1 = ZAlgorithm(SA1);

    vector<i64> sumA(N+1);
    rep(i,N) sumA[i+1] = A[i] + sumA[i];

    modint ans = 0;
    modint inv2 = modint(2).inv();
    rep(i,N){
        if(S[i] != S[0]) continue;
        i64 l = min(A[0], A[i]);
        ans += modint(l) * modint(l+1) * inv2;
        if(A[0] < A[i]){
            ans += modint(A[0]) * modint(A[i]-A[0]);
        }
        //cout << "l = " << l << '\n';
        //cout << "ans = " << ans.val() << '\n';
        if(i != N-1 && A[0] <= A[i]){
            i64 lp = 1;
            i64 rp = 1 + ZSA1[i];
            ans += modint(sumA[rp] - sumA[lp]);
            //cout << "dans = " << modint(sumA[rp] - sumA[lp]).val() << endl;
            if(i+1+ZSA1[i] != N){
                if(S[1+ZSA1[i]] == S[1+i+ZSA1[i]]){
                    ans += modint(min(A[1+ZSA1[i]], A[1+i+ZSA1[i]]));
                }
            }
        }
    }

    cout << ans.val() << '\n';
    return 0;
}




struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0