#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=200007;
ll DP[N][3];
int cnt[300];
int n,q;
int ile[300][2];
void get()
{
int a=cnt['C'],b=cnt['Z'],c=cnt['N'];
int p=(2*(c+b-a)%3+3)%3;
int ans=DP[c][p];
if(n>1&&n%2==1)
{
if(ile['C'][0]==0&&ile['Z'][1]==0) ans=(ans-1+mod)%mod;
if(ile['Z'][0]==0&&ile['C'][1]==0) ans=(ans-1+mod)%mod;
}
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
string s;
cin>>s;
DP[0][1]=1,DP[0][2]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<3;j++) DP[i][j]=(DP[i-1][j]+DP[i-1][(j+2)%3])%mod;
}
for(int i=0;i<n;i++)
{
cnt[s[i]]++;
ile[s[i]][i%2]++;
}
get();
while(q--)
{
int i;
char c;
cin>>i>>c;
i--;
cnt[s[i]]--;
ile[s[i]][i%2]--;
s[i]=c;
cnt[s[i]]++;
ile[s[i]][i%2]++;
get();
}
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 | #include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=200007; ll DP[N][3]; int cnt[300]; int n,q; int ile[300][2]; void get() { int a=cnt['C'],b=cnt['Z'],c=cnt['N']; int p=(2*(c+b-a)%3+3)%3; int ans=DP[c][p]; if(n>1&&n%2==1) { if(ile['C'][0]==0&&ile['Z'][1]==0) ans=(ans-1+mod)%mod; if(ile['Z'][0]==0&&ile['C'][1]==0) ans=(ans-1+mod)%mod; } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>q; string s; cin>>s; DP[0][1]=1,DP[0][2]=1; for(int i=1;i<=n;i++) { for(int j=0;j<3;j++) DP[i][j]=(DP[i-1][j]+DP[i-1][(j+2)%3])%mod; } for(int i=0;i<n;i++) { cnt[s[i]]++; ile[s[i]][i%2]++; } get(); while(q--) { int i; char c; cin>>i>>c; i--; cnt[s[i]]--; ile[s[i]][i%2]--; s[i]=c; cnt[s[i]]++; ile[s[i]][i%2]++; get(); } return 0; } |
English