結果

問題 No.1848 Long Prefixes
ユーザー SSRSSSRS
提出日時 2022-02-18 23:03:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,598 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 206,156 KB
実行使用メモリ 10,020 KB
最終ジャッジ日時 2023-09-11 19:59:58
合計ジャッジ時間 5,391 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
9,832 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 83 ms
9,860 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 77 ms
9,280 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 79 ms
9,304 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 76 ms
9,136 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 WA -
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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 << (long long) A[0] * (A[0] + 1) / 2 % MOD << 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] + 1];
          int x = Z[i - 1] + 1;
          int y = Z[i - 1] + i + 1;
          if (y < N){
            if (S[x] == S[y]){
              ans += min(A[x], A[y]);
            }
          }
        }
      }
      ans %= MOD;
    }
    ans %= MOD;
    cout << ans << endl;
  }
}
0