ARC094F Normalization

原题链接:https://beta.atcoder.jp/contests/arc094/tasks/arc094_d

Normalization

Problem Statement

You are given a string S S consisting of ‘a’,’b’ and ‘c’. Find the number of strings that can be possibly obtained by repeatedly performing the following operation zero or more times, modulo 998244353:

Choose an integer i i such that 1i|S|1 and the i i -th and (i+1)-th characters in S S are different. Replace each of the i-th and (i+1) ( i + 1 ) -th characters in S S with the character that differs from both of them (among a, b and c).

Constraints

2|S|2×105
S S consists of ‘a’, ‘b’ and ‘c’.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of strings that can be possibly obtained by repeatedly performing the operation, modulo 998244353 998244353 .

Sample Input 1

abc

Sample Output 1

3
abc, aaa and ccc can be obtained.

Sample Input 2

abbac

Sample Output 2

65

Sample Input 3

babacabac

Sample Output 3

6310

Sample Input 4

ababacbcacbacacbcbbcbbacbaccacbacbacba

Sample Output 4

148010497

题目大意

给定一个由 a,b,c a , b , c 组成的字符串 S|S|<=2×105 S ( | S | <= 2 × 10 5 ) ,每次可以选择两个相邻的不同字符,把它们修改成与两者都不同的字符,求能得到的不同的字符串的个数。

题解

%%%watson,watson is so strong!!! % % % w a t s o n , w a t s o n   i s   s o   s t r o n g ! ! !

感觉这道题真神(虽然watson说很水),如果把 a,b,c a , b , c 表示为 0,1,2 0 , 1 , 2 ,可以发现上述替换操作在 mod 3 m o d   3 意义下是不改变整个序列的和的。

然后开始 dp d p dp[i][j][k][p] d p [ i ] [ j ] [ k ] [ p ] 表示在第 i i 个位置,前缀和为j(mod 3),当前字符为 k k ,有/无连续的相同字符时的字符个数,枚举一下i,i+1的字符分别是什么就可以转移了。

最后的答案为 dp[n][tot][0][1]+dp[n][tot][1][1]+dp[n][tot][2][1]+[] d p [ n ] [ t o t ] [ 0 ] [ 1 ] + d p [ n ] [ t o t ] [ 1 ] [ 1 ] + d p [ n ] [ t o t ] [ 2 ] [ 1 ] + [ 原 串 没 有 连 续 的 相 同 字 母 ]

代码
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5,mod=998244353;
char ch[M];
int n,sum,i,j,k,a,b,dp[M][3][3][2];
bool flag=1;
void spj()
{
    for(i=2;i<=n;++i)if(ch[i]!=ch[i-1]){flag=0;break;}
    if(flag)puts("1"),exit(0);
    if(n<=3)(n==2?puts("2"):((ch[1]!=ch[2]&&ch[1]!=ch[3]&&ch[2]!=ch[3])?puts("3"):((ch[1]==ch[3])?puts("7"):puts("6")))),exit(0);
}
bool check(){for(int i=1;i<=n;++i)if(ch[i]==ch[i-1])return 0;return 1;}
void in(){scanf("%s",ch+1);}
void ac()
{
    n=strlen(ch+1);spj();
    for(i=1;i<=n;++i)sum=(sum+ch[i]-'a')%3;
    for(i=0;i<=2;++i)dp[1][i][i][0]=1;
    for(i=1;i<=n-1;++i)for(j=0;j<=2;++j)for(k=0;k<=2;++k)for(a=0;a<=1;++a)for(b=0;b<=2;++b)
    (dp[i+1][(j+b)%3][b][a|(k==b)]+=dp[i][j][k][a])%=mod;
    printf("%d",(0ll+dp[n][sum][0][1]+dp[n][sum][1][1]+dp[n][sum][2][1]+check())%mod);
}
int main(){in();ac();}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值