結果
問題 |
No.1845 Long Substrings
|
ユーザー |
👑 ![]() |
提出日時 | 2025-04-17 11:03:54 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 26 ms / 2,000 ms |
コード長 | 1,450 bytes |
コンパイル時間 | 2,075 ms |
コンパイル使用メモリ | 197,436 KB |
実行使用メモリ | 7,844 KB |
最終ジャッジ日時 | 2025-04-17 11:04:00 |
合計ジャッジ時間 | 5,060 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 34 |
ソースコード
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; // safe addition inline int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } // safe subtraction inline int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } // safe multiplication inline long long mul(long long a, long long b) { return (a * b) % MOD; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<long long> A(N); for(int i = 0; i < N; i++){ cin >> A[i]; } string S; cin >> S; // dp = number of distinct subsequences (including empty) so far int dp = 1; // last[c] = dp-value just before we last processed character c vector<int> last(26, 0); for(int i = 0; i < N; i++){ int c = S[i] - 'a'; long long L = A[i] % MOD; int dp_before = dp; int last_dp = last[c]; // dp at position just before this run // inc0 = dp_before - last_dp int inc0 = sub(dp_before, last_dp); // dp = dp_before + L * inc0 dp = add(dp_before, (int)mul(L, inc0)); // update last[c] = dp_{L-1} = dp_before + (L-1)*inc0 long long L1 = (A[i] - 1) % MOD; if (L1 < 0) L1 += MOD; last[c] = add(dp_before, (int)mul(L1, inc0)); } // subtract the empty subsequence int ans = sub(dp, 1); cout << ans << "\n"; return 0; }