F - ARC Stamp Editorial /

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 10001000

問題文

A, R, C からなる文字列 SS から始めて、「連続する三文字を選んで ARC に上書きする」操作を KK 回以下行ったところ、文字列 TT が得られました。

さて、最初の文字列 SS として何通りがあり得るでしょうか?これを 998244353998244353 で割った余りを求めてください。

制約

  • 3T50003 \leq |T| \leq 5000
  • 0K100000 \leq K \leq 10000
  • TTA, R, C からなる文字列

入力

入力は以下の形式で標準入力から与えられます。

TT
KK

出力

答えを出力してください。


入力例 1Copy

Copy
ARCCARC
1

出力例 1Copy

Copy
53

例えば、最初の文字列 SS が次のようなとき、11 回以下の操作で文字列 TT = ARCCARC を得ることができます。

  • SS = ARCCARC のとき:操作を行わないでも文字列 ARCCARC が得られる.
  • SS = CRACARC のとき:SS1,2,31, 2, 3 文字目を選んで ARC に上書きすると、文字列 ARCCARC が得られる。
  • SS = ARCCCCC のとき:SS5,6,75, 6, 7 文字目を選んで ARC に上書きすると、文字列 ARCCARC が得られる。

上に挙げたもの以外にもたくさんあり、SS としてあり得るものは全部で 5353 通りです。


入力例 2Copy

Copy
ARARCRCA
5

出力例 2Copy

Copy
2187

最初の文字列 SSAAAAAAAA のとき、例えば次のような 55 回以下の操作で文字列 TT = ARARCRCA を得ることができます。

  • ステップ 11:まず、3,4,53, 4, 5 文字目を選んで ARC に上書きすると、文字列 AAARCAAA が得られる。
  • ステップ 22:次に、5,6,75, 6, 7 文字目を選んで ARC に上書きすると、文字列 AAARARCA が得られる。
  • ステップ 33:次に、1,2,31, 2, 3 文字目を選んで ARC に上書きすると、文字列 ARCRARCA が得られる。
  • ステップ 44:最後、3,4,53, 4, 5 文字目を選んで ARC に上書きすると、文字列 ARARCRCA が得られる。

それ以外にも SS としてあり得るものはたくさんあり、全部で 21872187 通りです。


入力例 3Copy

Copy
AARCRRARCC
0

出力例 3Copy

Copy
1

00 回の操作で文字列 TT = AARCRRARCC を得られるのは、最初の時点で S=TS = T のとき、すなわち SS = AARCRRARCC11 通りしかありません。


入力例 4Copy

Copy
AAAAARRRRRCCCCC
131

出力例 4Copy

Copy
1

この入力例では、SS としてあり得るものは AAAAARRRRRCCCCC11 通りだけです。


入力例 5Copy

Copy
CAARCACRAAARARARCRCRARCARARCRRARC
9

出力例 5Copy

Copy
797833187

最初の文字列 SS としてあり得るものは 320236026147320236026147 通りあるので、これを 998244353998244353 で割った余りである 797833187797833187 を出力してください。

Score : 10001000 points

Problem Statement

On a string SS consisting of A, R, and C, we did the following operation at most KK times: choose three consecutive characters and overwrite them with ARC. The result is the string TT.

How many strings are there that could be the initial string SS? Find this count modulo 998244353998244353.

Constraints

  • 3T50003 \leq |T| \leq 5000
  • 0K100000 \leq K \leq 10000
  • TT is a string consisting of A, R, and C.

Input

Input is given from Standard Input in the following format:

TT
KK

Output

Print the answer.


Sample Input 1Copy

Copy
ARCCARC
1

Sample Output 1Copy

Copy
53

Below are some examples of the initial string SS from which we can get the string TT = ARCCARC with at most 11 operation.

  • SS = ARCCARC: we can choose to do nothing to get ARCCARC.
  • SS = CRACARC: we can choose the 11-st, 22-nd, 33-rd characters and overwrite them with ARC to get ARCCARC.
  • SS = ARCCCCC: we can choose the 55-th, 66-th, 77-th characters and overwrite them with ARC to get ARCCARC.

There are many other strings that could be SS, for a total of 5353.


Sample Input 2Copy

Copy
ARARCRCA
5

Sample Output 2Copy

Copy
2187

If the intial string SS is AAAAAAAA, one way to get TT = ARARCRCA with at most 55 operations is as follows.

  • Step 11: choose the 33-rd, 44-th, 55-th characters and overwrite them with ARC to get the string AAARCAAA.
  • Step 22: choose the 55-th, 66-th, 77-th characters and overwrite them with ARC to get the string AAARARCA.
  • Step 33: choose the 11-st, 22-nd, 33-rd characters and overwrite them with ARC to get the string ARCRARCA.
  • Step 44: choose the 33-rd, 44-th, 55-th characters and overwrite them with ARC to get the string ARARCRCA.

There are many other strings that could be SS, for a total of 21872187.


Sample Input 3Copy

Copy
AARCRRARCC
0

Sample Output 3Copy

Copy
1

We can get TT = AARCRRARCC with 00 operations in only one case in which S=TS = T from the beginning, or SS = AARCRRARCC.


Sample Input 4Copy

Copy
AAAAARRRRRCCCCC
131

Sample Output 4Copy

Copy
1

In this case, there is just one string that could be SS: AAAAARRRRRCCCCC.


Sample Input 5Copy

Copy
CAARCACRAAARARARCRCRARCARARCRRARC
9

Sample Output 5Copy

Copy
797833187

There are 320236026147320236026147 strings that could be SS, so print this count modulo 998244353998244353, which is 797833187797833187.



2025-04-08 (Tue)
04:45:40 +00:00