結果

問題 No.1845 Long Substrings
ユーザー SSRS
提出日時 2022-02-18 22:22:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 2,708 ms
コンパイル使用メモリ 224,060 KB
最終ジャッジ日時 2025-01-28 00:17:24
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
struct binary_indexed_tree{
  int N;
  vector<long long> BIT;
  binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
  }
  void add(int i, long long x){
    i++;
    while (i <= N){
      BIT[i] += x;
      BIT[i] %= MOD;
      i += i & -i;
    }
  }
  long long sum(int i){
    long long ans = 0;
    while (i > 0){
      ans += BIT[i];
      ans %= MOD;
      i -= i & -i;
    }
    return ans;
  }
  long long sum(int L, int R){
    return (sum(R) - sum(L) + MOD) % MOD;
  }
};
long long modpow(long long a, long long b){
	long long ans = 1;
	while (b > 0){
		if (b % 2 == 1){
			ans *= a;
			ans %= MOD;
		}
		a *= a;
		a %= MOD;
		b /= 2;
	}
	return ans;
}
long long modinv(long long a){
	return modpow(a, MOD - 2);
}
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  string S;
  cin >> S;
  vector<int> last(26, -1);
  vector<int> lastA(26);
  binary_indexed_tree dp(N);
  for (int i = 0; i < N; i++){
    int c = S[i] - 'a';
    long long s;
    if (last[c] == -1){
      s = dp.sum(i);
    } else {
      s = dp.sum(last[c] + 1, i);
      s += dp.sum(last[c], last[c] + 1) * modinv(lastA[c]) % MOD;
    }
    if (last[c] == -1){
      s++;
    }
    s %= MOD;
    s *= A[i];
    s %= MOD;
    dp.add(i, s);
    lastA[c] = A[i];
    last[c] = i;
  }
  long long ans = dp.sum(0, N);
  cout << ans << endl;
}
0