問題一覧 > 通常問題

No.3458 Scores of Subsequence

レベル : / AI推定難易度 : 850 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 84
作問者 : くらげ / テスター : tyawanmusi dyktr_06 おのせ hikikomori
お気に入りにしたユーザー ProblemId : 13009 / 自分の提出
問題文最終更新日: 2026-02-27 23:40:00
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
MMA Contest 021 (順位表) の他の問題:

Problem Statement

For a string $T$, define the score $f(T)$ as follows.

  • If, for some non-negative integer $N$, $T$ is formed by concatenating $N$ copies of M and $1$ copy of A in this order, then $f(T) = 2^N$.
  • Otherwise, $f(T) = 0$.

For example, MMA is the concatenation of $2$ copies of M and $1$ copy of A in this order, so the score $f($ MMA $)$ is $4$.

You are given a string $S$ consisting only of M and A. Find the score of every subsequence of $S$, and compute their sum modulo $998244353$.

Here, a subsequence is a string obtained by removing $0$ or more characters from the original string and concatenating the remaining characters in their original order.

Constraints

  • $1 \le |S| \le 2 \times 10^5$
  • $S$ is a string consisting only of M and A.

Input

$S$

Output

Print the sum of the scores of all subsequences of $S$, modulo $998244353$, in $1$ line.

Sample

Sample 1
Input
MA
Output
3

The subsequences of MA are the $3$ strings M, A, and MA, whose scores are $0$, $1$, and $2$ respectively, so the answer is $3$ since $0+1+2=3$.

Sample 2
Input
MMMAMMA
Output
270

Among the subsequences of $S = $ MMMAMMA, those with a score of $1$ or more include A, MMA, and MMMMA, with $f($A$) = 1$, $f($MMA$) = 4$, and $f($MMMMA$) = 16$.

Sample 3
Input
MAMMMAMAMMMMMAMMMMMMMMAMMAMMMA
Output
189496499

Note that you should print the answer modulo $998244353$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。