結果

問題 No.1848 Long Prefixes
ユーザー 👑 tatyamtatyam
提出日時 2021-12-23 13:12:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,519 bytes
コンパイル時間 4,610 ms
コンパイル使用メモリ 282,604 KB
実行使用メモリ 17,364 KB
最終ジャッジ日時 2023-09-11 18:03:01
合計ジャッジ時間 11,440 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <atcoder/string>
#include <atcoder/modint>
using namespace std;
using Modint = atcoder::modint1000000007;

template<class T> vector<int> z_algorithm(vector<T> A){
    const int N = A.size();
    vector<int> a(N);
    {
        auto B = A;
        sort(B.begin(), B.end());
        for(int i = 0; i < N; i++) a[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();
    }
    vector<int> ans(N);
    for(int i = 0; i < N; i++){
        const int c = a[i];
        for(int j = i; j < N; j++){
            if(a[j] == c && ans[j - i] == i) ans[j - i]++;
        }
    }
    return ans;
}
int main(){
    int N;
    cin >> N;
    vector<pair<char, int>> A(N);
    for(auto& [c, a] : A) cin >> a;
    for(auto& [c, a] : A) cin >> c;
    auto Z = z_algorithm(vector(A.begin() + 1, A.end()));
    Z.push_back(0);
    vector<Modint> sum(N);
    sum[0] = Modint::raw(A[0].second);
    for(int i = 1; i < N; i++) sum[i] = sum[i - 1] + Modint::raw(A[i].second);
    const auto [c0, a0] = A[0];
    Modint ans = 0;
    for(int i = 0; i < N; i++) if(auto [c, a] = A[i]; c == c0){
        if(a < a0){
            ans += uint64_t(a) * (a + 1) / 2;
            continue;
        }
        ans += uint64_t(a0) * (a + a - a0 - 1) / 2;
        const int z = Z[i];
        ans += sum[z];
        if(i + 1 + z < N && A[i + 1 + z].first == A[1 + z].first){
            ans += Modint::raw(min(A[i + 1 + z].second, A[1 + z].second));
        }
    }
    cout << ans.val() << endl;
}
0