#include<cstdio> #include<set> #include<string> #include<iostream> #include<cstdlib> using namespace std; const int N=200005,M=555555,K=10,P=1000000007; const string state[K]={"C","Z","CC","ZZ","CZ","ZC","CZC","ZCZ","CCC","ZZZ"}; int n,q,i,j,combine[K][K],v[M][K],pos[N];char s[N]; string simplify(string s){ int n=s.size(); for(int i=0;i+1<n;i++)if(s[i]==s[i+1]){ if((i&&s[i-1]!=s[i])||(i+2<n&&s[i+1]!=s[i+2])){ string t; for(int j=0;j<i;j++)t.push_back(s[j]); t.push_back('C'+'Z'-s[i]); for(int j=i+2;j<n;j++)t.push_back(s[j]); return simplify(t); } } if(s[0]==s[1]){ while(n>4)n-=3; if(n==4){ n=2; for(int i=0;i<n;i++)s[i]='C'+'Z'-s[i]; } }else while(n>3)n-=2; s.resize(n); return s; } inline bool check(string s){ int n=s.size(); if(n==1)return 1; if(n==2)return s[0]==s[1]; return 0; } inline int getstate(string s){ for(int i=0;i<K;i++)if(state[i]==s)return i; return -1; } inline void init(int x,char p){ for(int i=0;i<K;i++)v[x][i]=0; if(p=='C'||p=='N')v[x][0]=1; if(p=='Z'||p=='N')v[x][1]=1; } inline void merge(int*a,int*b,int*c){ for(int i=0;i<K;i++)c[i]=0; for(int i=0;i<K;i++)if(a[i])for(int j=0;j<K;j++)if(b[j]){ int k=combine[i][j]; c[k]=(c[k]+1LL*a[i]*b[j])%P; } } void build(int x,int a,int b){ if(a==b){ init(pos[a]=x,s[a]); return; } int mid=(a+b)>>1; build(x<<1,a,mid); build(x<<1|1,mid+1,b); merge(v[x<<1],v[x<<1|1],v[x]); } inline void modify(int x,char p){ init(x=pos[x],p); for(x>>=1;x;x>>=1)merge(v[x<<1],v[x<<1|1],v[x]); } inline void getans(){ int ans=0; for(int i=0;i<4;i++)(ans+=v[1][i])%=P; printf("%d\n",ans); } namespace FIND{ set<string>T; set<string>F; const int LIM=16; int cnt; void dfs(string s){ if(!check(simplify(s))){ puts("WA"); cout<<s<<" "<<simplify(s)<<endl; exit(0); } if(s.size()>LIM)return; if(T.find(s)!=T.end())return; if(s.size()==LIM)cnt++; T.insert(s); for(int i=0;i<s.size();i++){ string t; for(int j=0;j<i;j++)t.push_back(s[j]); if(s[i]=='C'){ t+="ZZ"; }else{ t+="CC"; } for(int j=i+1;j<s.size();j++)t.push_back(s[j]); dfs(t); } } void go(string s){ if(s.size()>LIM)return; F.insert(simplify(s)); s.push_back('C'); go(s); s[s.size()-1]='Z'; go(s); } void gao(){ dfs("C"); dfs("Z"); puts("DONE"); printf("%d\n",cnt); /*for(set<string>::iterator it=T.begin();it!=T.end();it++){ cout<<*it<<endl; }*/ go("C"); go("Z"); for(set<string>::iterator it=F.begin();it!=F.end();it++){ cout<<*it<<endl; } } } int main(){ /*string s; cin>>s; cout<<simplify(s);*/ //FIND::gao(); for(i=0;i<K;i++)for(j=0;j<K;j++)combine[i][j]=getstate(simplify(state[i]+state[j])); scanf("%d%d%s",&n,&q,s+1); build(1,1,n); getans(); while(q--){ scanf("%d%s",&i,s); modify(i,s[0]); getans(); } }
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include<cstdio> #include<set> #include<string> #include<iostream> #include<cstdlib> using namespace std; const int N=200005,M=555555,K=10,P=1000000007; const string state[K]={"C","Z","CC","ZZ","CZ","ZC","CZC","ZCZ","CCC","ZZZ"}; int n,q,i,j,combine[K][K],v[M][K],pos[N];char s[N]; string simplify(string s){ int n=s.size(); for(int i=0;i+1<n;i++)if(s[i]==s[i+1]){ if((i&&s[i-1]!=s[i])||(i+2<n&&s[i+1]!=s[i+2])){ string t; for(int j=0;j<i;j++)t.push_back(s[j]); t.push_back('C'+'Z'-s[i]); for(int j=i+2;j<n;j++)t.push_back(s[j]); return simplify(t); } } if(s[0]==s[1]){ while(n>4)n-=3; if(n==4){ n=2; for(int i=0;i<n;i++)s[i]='C'+'Z'-s[i]; } }else while(n>3)n-=2; s.resize(n); return s; } inline bool check(string s){ int n=s.size(); if(n==1)return 1; if(n==2)return s[0]==s[1]; return 0; } inline int getstate(string s){ for(int i=0;i<K;i++)if(state[i]==s)return i; return -1; } inline void init(int x,char p){ for(int i=0;i<K;i++)v[x][i]=0; if(p=='C'||p=='N')v[x][0]=1; if(p=='Z'||p=='N')v[x][1]=1; } inline void merge(int*a,int*b,int*c){ for(int i=0;i<K;i++)c[i]=0; for(int i=0;i<K;i++)if(a[i])for(int j=0;j<K;j++)if(b[j]){ int k=combine[i][j]; c[k]=(c[k]+1LL*a[i]*b[j])%P; } } void build(int x,int a,int b){ if(a==b){ init(pos[a]=x,s[a]); return; } int mid=(a+b)>>1; build(x<<1,a,mid); build(x<<1|1,mid+1,b); merge(v[x<<1],v[x<<1|1],v[x]); } inline void modify(int x,char p){ init(x=pos[x],p); for(x>>=1;x;x>>=1)merge(v[x<<1],v[x<<1|1],v[x]); } inline void getans(){ int ans=0; for(int i=0;i<4;i++)(ans+=v[1][i])%=P; printf("%d\n",ans); } namespace FIND{ set<string>T; set<string>F; const int LIM=16; int cnt; void dfs(string s){ if(!check(simplify(s))){ puts("WA"); cout<<s<<" "<<simplify(s)<<endl; exit(0); } if(s.size()>LIM)return; if(T.find(s)!=T.end())return; if(s.size()==LIM)cnt++; T.insert(s); for(int i=0;i<s.size();i++){ string t; for(int j=0;j<i;j++)t.push_back(s[j]); if(s[i]=='C'){ t+="ZZ"; }else{ t+="CC"; } for(int j=i+1;j<s.size();j++)t.push_back(s[j]); dfs(t); } } void go(string s){ if(s.size()>LIM)return; F.insert(simplify(s)); s.push_back('C'); go(s); s[s.size()-1]='Z'; go(s); } void gao(){ dfs("C"); dfs("Z"); puts("DONE"); printf("%d\n",cnt); /*for(set<string>::iterator it=T.begin();it!=T.end();it++){ cout<<*it<<endl; }*/ go("C"); go("Z"); for(set<string>::iterator it=F.begin();it!=F.end();it++){ cout<<*it<<endl; } } } int main(){ /*string s; cin>>s; cout<<simplify(s);*/ //FIND::gao(); for(i=0;i<K;i++)for(j=0;j<K;j++)combine[i][j]=getstate(simplify(state[i]+state[j])); scanf("%d%d%s",&n,&q,s+1); build(1,1,n); getans(); while(q--){ scanf("%d%s",&i,s); modify(i,s[0]); getans(); } } |