#C240915B. 神奇的字母


#C240915B. 神奇的字母

网址(Website)

题目详情 - 神奇的字母 - Super

题解 - 神奇的字母 - Super

题解(Solution)

原题见: AGC027E

这个人的题解很不错,推荐推荐: AGC027E题解

代码(Code)

#include<bits/stdc++.h>
#define For(i,l,r) for(int i=l;i<=r;i++)
#define Rof(i,l,r) for(int i=l;i>=r;i--)
using namespace std; 
#define int long long
#define x first
#define y second
inline int rd(){int x;return cin>>x,x;}
const int inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7; 
const int N = 201234;
bool ST;

int f[N],lst[N];
int n,a[N];
char s[N];

bool ED;
//多测清空 
signed main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cerr<<abs(&ST-&ED)/1024./1024.<<"MB\n";
	freopen("alpha.in","r",stdin);
	freopen("alpha.out","w",stdout); 
	int T = rd(), Tim = clock();
	while(T--){
		bool flag = false;//可不可以分段 
		cin>>(s+1);
		n = strlen(s+1);
		
		For(i,2,n) flag |= s[i]==s[i-1];
		if(!flag){cout<<1<<'\n';continue;}
		
		For(i,1,n) a[i] = s[i]=='a'?1:2;
		For(i,1,n) a[i] = (a[i-1]+a[i])%3;
		For(i,1,n){
			if(a[i]>=1) f[i] = 1;
			For(j,0,2){
				if(j!=a[i]) (f[i]+=f[lst[j]])%=mod;
			}lst[a[i]] = i;
		}cout<<f[n]<<'\n';
		
		For(j,0,2) lst[j] = 0;
		For(i,1,n) a[i] = f[i] = 0; 
	}return cerr<<"TIME:"<<(clock()-Tim)/1000.,0;
}

文章作者: WolfDeer
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 WolfDeer !
  目录