結果

問題 No.1848 Long Prefixes
ユーザー SSRSSSRS
提出日時 2022-02-18 23:09:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,556 bytes
コンパイル時間 1,688 ms
コンパイル使用メモリ 174,268 KB
実行使用メモリ 10,092 KB
最終ジャッジ日時 2023-09-11 20:02:04
合計ジャッジ時間 4,685 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 39 ms
9,764 KB
testcase_02 AC 84 ms
9,748 KB
testcase_03 AC 83 ms
9,728 KB
testcase_04 AC 39 ms
9,744 KB
testcase_05 AC 82 ms
9,256 KB
testcase_06 WA -
testcase_07 AC 38 ms
9,180 KB
testcase_08 AC 79 ms
9,200 KB
testcase_09 WA -
testcase_10 AC 39 ms
9,508 KB
testcase_11 AC 80 ms
9,504 KB
testcase_12 WA -
testcase_13 AC 37 ms
9,252 KB
testcase_14 AC 80 ms
9,484 KB
testcase_15 WA -
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 35 ms
5,736 KB
testcase_34 AC 35 ms
7,988 KB
testcase_35 AC 17 ms
4,436 KB
testcase_36 AC 32 ms
5,760 KB
testcase_37 AC 30 ms
6,980 KB
testcase_38 AC 45 ms
6,524 KB
testcase_39 AC 34 ms
5,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
vector<int> z_algorithm(vector<pair<char, int>> S){
  int N = S.size();
  vector<int> ans(N);
  for (int i = 1, j = 0; i < N; i++){
    if (i + ans[i - j] < j + ans[j]){
      ans[i] = ans[i - j];
    } else {
      int k = max(0, j + ans[j] - i);
      while (i + k < N && S[k] == S[i + k]){
        k++;
      }
      ans[i] = k;
      j = i;
    }
  }
  ans[0] = N;
  return ans;
}
long long tri(long long n){
  return n * (n + 1) / 2 % MOD;
}
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  string S;
  cin >> S;
  if (N == 1){
    cout << tri(A[0]) << endl;
  } else {
    vector<long long> sum(N + 1);
    sum[0] = 0;
    for (int i = 0; i < N; i++){
      sum[i + 1] = sum[i] + A[i];
    }
    vector<pair<char, int>> T(N - 1);
    for (int i = 0; i < N - 1; i++){
      T[i] = make_pair(A[i + 1], S[i + 1]);
    }
    vector<int> Z = z_algorithm(T);
    long long ans = sum[N] + tri(A[0] - 1);
    for (int i = 1; i < N; i++){
      if (S[i] == S[0]){
        if (A[i] < A[0]){
          ans += tri(A[i]);
        } else {
          ans += tri(A[0] - 1);
          ans += (long long) A[0] * (A[i] - A[0]) % MOD;
          ans += sum[Z[i] + 1];
          int x = Z[i] + 1;
          int y = Z[i] + i + 1;
          if (y < N){
            if (S[x] == S[y]){
              ans += min(A[x], A[y]);
            }
          }
        }
      }
      ans %= MOD;
    }
    ans %= MOD;
    cout << ans << endl;
  }
}
0