結果
問題 |
No.1845 Long Substrings
|
ユーザー |
![]() |
提出日時 | 2022-02-18 22:20:59 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,111 bytes |
コンパイル時間 | 3,261 ms |
コンパイル使用メモリ | 221,976 KB |
最終ジャッジ日時 | 2025-01-28 00:16:28 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 9 WA * 25 |
ソースコード
#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; } }; 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); 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], i); } if (last[c] == -1){ s++; } s %= MOD; s *= A[i]; s %= MOD; dp.add(i, s); last[c] = i; } long long ans = dp.sum(0, N); cout << ans << endl; }