#include <bits/stdc++.h> using namespace std; #pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") typedef long long LL; const LL MOD = 998244353; LL dp[50005]; LL l[8]; LL p[8]; int n, q; string s; LL f(){ dp[1]=1; for(int i=0; i<8; i++) l[i] = p[i] = 0; for(int i=2; i<n; i++){ int x = s[i]-'a'; dp[i] = (dp[i-1] * 2 - l[x] + MOD) % MOD; l[x] = dp[i-1]; } int m = 0; for(int i=0; i<8; i++){ if(l[i])m=i+1; } vector<pair<LL, LL>> d(m); for(int i=2; i<n; i++){ int x = s[i]-'a'; if(p[x] == 0){ p[x]=i; d[x].first++; } LL sum = 0; for(int j=0; j<m; j++){ if(d[j].second > d[x].second) sum += d[j].first; } d[x].first = (d[x].first + sum) % MOD; d[x].second = i; } LL sum = 0; for(int j=0; j<m; j++) sum += d[j].first; return (dp[n-1] - sum%MOD - 1 + MOD) % MOD; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>q; cin>>s; s = ".." + s; n++; n++; cout<<f()<<"\n"; while(q--){ int id; char c; cin>>id>>c; s[id+1] = c; cout<<f()<<"\n"; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <bits/stdc++.h> using namespace std; #pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") typedef long long LL; const LL MOD = 998244353; LL dp[50005]; LL l[8]; LL p[8]; int n, q; string s; LL f(){ dp[1]=1; for(int i=0; i<8; i++) l[i] = p[i] = 0; for(int i=2; i<n; i++){ int x = s[i]-'a'; dp[i] = (dp[i-1] * 2 - l[x] + MOD) % MOD; l[x] = dp[i-1]; } int m = 0; for(int i=0; i<8; i++){ if(l[i])m=i+1; } vector<pair<LL, LL>> d(m); for(int i=2; i<n; i++){ int x = s[i]-'a'; if(p[x] == 0){ p[x]=i; d[x].first++; } LL sum = 0; for(int j=0; j<m; j++){ if(d[j].second > d[x].second) sum += d[j].first; } d[x].first = (d[x].first + sum) % MOD; d[x].second = i; } LL sum = 0; for(int j=0; j<m; j++) sum += d[j].first; return (dp[n-1] - sum%MOD - 1 + MOD) % MOD; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>q; cin>>s; s = ".." + s; n++; n++; cout<<f()<<"\n"; while(q--){ int id; char c; cin>>id>>c; s[id+1] = c; cout<<f()<<"\n"; } return 0; } |