#abc359d. D - Avoid K Palindrome

D - Avoid K Palindrome

Score : 450450 points

问题陈述

你得到了一个由字符 AB? 组成的长度为 NN 的字符串 SS

你还得到了一个正整数 KK。如果一个由 AB 组成的字符串 TT 满足以下条件,则被认为是一个好字符串

  • 没有长度为 KK 的连续子串在 TT 中是回文。

qqSS? 字符的数量。通过将 SS 中的每个 ? 替换为 AB,可以得到 2q2^q 个字符串。找出这些字符串中有多少个是好字符串。

计数可能会非常大,因此需要找到它对 998244353998244353 取模的结果。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a string SS of length NN consisting of characters A, B, and ?.

You are also given a positive integer KK. A string TT consisting of A and B is considered a good string if it satisfies the following condition:

  • No contiguous substring of length KK in TT is a palindrome.

Let qq be the number of ? characters in SS. There are 2q2^q strings that can be obtained by replacing each ? in SS with either A or B. Find how many of these strings are good strings.

The count can be very large, so find it modulo 998244353998244353.

Constraints

  • 2KN10002 \leq K \leq N \leq 1000
  • K10K \leq 10
  • SS is a string consisting of A, B, and ?.
  • The length of SS is NN.
  • NN and KK are integers.

Input

The input is given from Standard Input in the following format:

NN KK

SS

Output

Print the answer.

Sample Input 1

7 4
AB?A?BA

Sample Output 1

1

The given string has two ?s. There are four strings obtained by replacing each ? with A or B:

  • ABAAABA
  • ABAABBA
  • ABBAABA
  • ABBABBA

Among these, the last three contain the contiguous substring ABBA of length 4, which is a palindrome, and thus are not good strings.

Therefore, you should print 1.

Sample Input 2

40 7
????????????????????????????????????????

Sample Output 2

116295436

Ensure to find the number of good strings modulo 998244353998244353.

Sample Input 3

15 5
ABABA??????????

Sample Output 3

0

It is possible that there is no way to replace the ?s to obtain a good string.

Sample Input 4

40 8
?A?B??B?B?AA?A?B??B?A???B?BB?B???BA??BAA

Sample Output 4

259240
}