#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <int, int> pii; #define se second #define fi first const int N = 2e5; const int mod = 1e9 + 7; int n, q, c, z, k; char a[N + 5]; ll pw[N + 5]; char y; ll res; bool t; unordered_set <int> s; ll Solve(bool t){ if(n == 1){ return (a[0] == 'N' ? 2 : 1); } if(!t and c + z == n){ return 0; } int r = (c - z) % 3; if(c == 0 and z == 0){ return ((pw[n - c - z + 1] + (n % 2 ? -2 : 2)) / 3 - !(n % 2)) % mod; } if(!r){ //cout << "bla\n"; return ((pw[n - c - z + 1] + ((n - c - z) % 2 ? 2 : -2)) / 3 - !t) % mod; }else{ return (pw[n - c - z] - (pw[n - c - z] / 3 + (pw[n - c - z] % 3 == 1 ? 0 : 1))) % mod; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> q; pw[0] = 1ll; for(int i=1; i<=n+1; i++){ pw[i] = 2ll * pw[i - 1]; pw[i] %= mod; } cin >> a[0]; c += (a[0] == 'C'); z += (a[0] == 'Z'); for(int i=1; i<n; i++){ cin >> a[i]; c += (a[i] == 'C'); z += (a[i] == 'Z'); if(a[i] != 'N' and a[i - 1] == a[i]){ t = true; s.insert(i); } } cout << Solve(t) << "\n"; while(q --){ cin >> k >> y; k --; if(s.find(k + 1) != s.end()){ s.erase(k + 1); } if(y != 'N'){ if(k > 0 and a[k - 1] == y){ s.insert((k)); } if(k < n - 1 and a[k + 1] == y){ s.insert(k + 1); } } t = (s.size() > 0); if(y == 'C'){ c ++; z -= (a[k] == 'Z'); } if(y == 'Z'){ z ++; c -= (a[k] == 'C'); } if(y == 'N'){ c -= (a[k] == 'C'); z -= (a[k] == 'Z'); } a[k] = y; cout << Solve(t) << "\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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <int, int> pii; #define se second #define fi first const int N = 2e5; const int mod = 1e9 + 7; int n, q, c, z, k; char a[N + 5]; ll pw[N + 5]; char y; ll res; bool t; unordered_set <int> s; ll Solve(bool t){ if(n == 1){ return (a[0] == 'N' ? 2 : 1); } if(!t and c + z == n){ return 0; } int r = (c - z) % 3; if(c == 0 and z == 0){ return ((pw[n - c - z + 1] + (n % 2 ? -2 : 2)) / 3 - !(n % 2)) % mod; } if(!r){ //cout << "bla\n"; return ((pw[n - c - z + 1] + ((n - c - z) % 2 ? 2 : -2)) / 3 - !t) % mod; }else{ return (pw[n - c - z] - (pw[n - c - z] / 3 + (pw[n - c - z] % 3 == 1 ? 0 : 1))) % mod; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> q; pw[0] = 1ll; for(int i=1; i<=n+1; i++){ pw[i] = 2ll * pw[i - 1]; pw[i] %= mod; } cin >> a[0]; c += (a[0] == 'C'); z += (a[0] == 'Z'); for(int i=1; i<n; i++){ cin >> a[i]; c += (a[i] == 'C'); z += (a[i] == 'Z'); if(a[i] != 'N' and a[i - 1] == a[i]){ t = true; s.insert(i); } } cout << Solve(t) << "\n"; while(q --){ cin >> k >> y; k --; if(s.find(k + 1) != s.end()){ s.erase(k + 1); } if(y != 'N'){ if(k > 0 and a[k - 1] == y){ s.insert((k)); } if(k < n - 1 and a[k + 1] == y){ s.insert(k + 1); } } t = (s.size() > 0); if(y == 'C'){ c ++; z -= (a[k] == 'Z'); } if(y == 'Z'){ z ++; c -= (a[k] == 'C'); } if(y == 'N'){ c -= (a[k] == 'C'); z -= (a[k] == 'Z'); } a[k] = y; cout << Solve(t) << "\n"; } return 0; } |