#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define irep(i,a,b) for(int i = a; i >= b; i--)
typedef long long ll;
typedef long double ld;
//typedef __int128 int128;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<ll,ll> pl;
const ll mod = 998'244'353;
const int max_n = 5e4+7;
int arr[max_n];
ll dp[max_n];
ll dp2[max_n][2];
ll last[max_n];
map<string,int> m;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,q; cin >> n >> q;
string s; cin >> s;
bool check_ab = 1;
rep(i,0,n-1) if(s[i] != 'a' && s[i] != 'b') check_ab = 0;
if(check_ab){
while(1){
//cout << "nowy test\n";
//cout << "arr: "; rep(i,1,n) cout << arr[i]; cout << '\n';
ll p[2] = {0,0};
rep(i,1,n){
arr[i] = s[i-1]-'a';
last[i] = p[arr[i]];
p[arr[i]] = i;
//cout << "i: " << i << " last: " << last[i] << '\n';
}
//podzial na grupy!!!
int ile_gr = 1;
rep(i,2,n) if(arr[i] != arr[i-1]) ile_gr++;
ll sum[2] = {0,0};
rep(i,1,n){
dp[i] = sum[arr[i]^1]+dp[last[i]];
if(last[i] == 0) dp[i]++;
if(dp[i] >= mod) dp[i] -= mod;
sum[arr[i]^1] = 0;
sum[arr[i]] += dp[i]; if(sum[arr[i]] >= mod) sum[arr[i]] -= mod;
//cout << "i: " << i << " dp: " << dp[i] << '\n';
}
ll ans = 0;
rep(i,1,n) ans += dp[i];
dp2[1][0] = 1; dp2[1][1] = 1;
rep(i,2,ile_gr){
dp2[i][0] = dp2[i-1][1];
dp2[i][1] = dp2[i-1][0]+dp2[i-1][1]; if(dp2[i][1] > mod) dp2[i][1] -= mod;
}
ans = (ans-dp2[ile_gr][0]-dp2[ile_gr][1]+2*mod)%mod;
if(ile_gr == 1) ans++; if(ans >= mod) ans -= mod; //wtedy check na 1 grupe!!!
cout << ans << '\n';
if(q == 0) break;
q--;
int a; char c;
cin >> a >> c; s[a-1] = c;
}
}
else{
while(1){
rep(mask,0,(1<<n)-1){
string x;
rep(i,0,n-1) if(mask&(1<<i)) x += s[i];
m[x]++;
}
int ans = 0;
for(auto x:m) if(x.nd > 1) ans++;
cout << ans << '\n';
if(q == 0) break;
q--;
int a; char c;
cin >> a >> c; s[a-1] = c;
m.clear();
}
}
return 0;
}
//g++ -O3 -static -Wall .cpp -std=c++17
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define rep(i,a,b) for(int i = a; i <= b; i++) #define irep(i,a,b) for(int i = a; i >= b; i--) typedef long long ll; typedef long double ld; //typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const ll mod = 998'244'353; const int max_n = 5e4+7; int arr[max_n]; ll dp[max_n]; ll dp2[max_n][2]; ll last[max_n]; map<string,int> m; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,q; cin >> n >> q; string s; cin >> s; bool check_ab = 1; rep(i,0,n-1) if(s[i] != 'a' && s[i] != 'b') check_ab = 0; if(check_ab){ while(1){ //cout << "nowy test\n"; //cout << "arr: "; rep(i,1,n) cout << arr[i]; cout << '\n'; ll p[2] = {0,0}; rep(i,1,n){ arr[i] = s[i-1]-'a'; last[i] = p[arr[i]]; p[arr[i]] = i; //cout << "i: " << i << " last: " << last[i] << '\n'; } //podzial na grupy!!! int ile_gr = 1; rep(i,2,n) if(arr[i] != arr[i-1]) ile_gr++; ll sum[2] = {0,0}; rep(i,1,n){ dp[i] = sum[arr[i]^1]+dp[last[i]]; if(last[i] == 0) dp[i]++; if(dp[i] >= mod) dp[i] -= mod; sum[arr[i]^1] = 0; sum[arr[i]] += dp[i]; if(sum[arr[i]] >= mod) sum[arr[i]] -= mod; //cout << "i: " << i << " dp: " << dp[i] << '\n'; } ll ans = 0; rep(i,1,n) ans += dp[i]; dp2[1][0] = 1; dp2[1][1] = 1; rep(i,2,ile_gr){ dp2[i][0] = dp2[i-1][1]; dp2[i][1] = dp2[i-1][0]+dp2[i-1][1]; if(dp2[i][1] > mod) dp2[i][1] -= mod; } ans = (ans-dp2[ile_gr][0]-dp2[ile_gr][1]+2*mod)%mod; if(ile_gr == 1) ans++; if(ans >= mod) ans -= mod; //wtedy check na 1 grupe!!! cout << ans << '\n'; if(q == 0) break; q--; int a; char c; cin >> a >> c; s[a-1] = c; } } else{ while(1){ rep(mask,0,(1<<n)-1){ string x; rep(i,0,n-1) if(mask&(1<<i)) x += s[i]; m[x]++; } int ans = 0; for(auto x:m) if(x.nd > 1) ans++; cout << ans << '\n'; if(q == 0) break; q--; int a; char c; cin >> a >> c; s[a-1] = c; m.clear(); } } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |
English