CF1499E.Chaotic Merge

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two strings xx and yy , both consist only of lowercase Latin letters. Let s|s| be the length of string ss .

Let's call a sequence aa a merging sequence if it consists of exactly x|x| zeros and exactly y|y| ones in some order.

A merge zz is produced from a sequence aa by the following rules:

  • if ai=0a_i=0 , then remove a letter from the beginning of xx and append it to the end of zz ;
  • if ai=1a_i=1 , then remove a letter from the beginning of yy and append it to the end of zz .

Two merging sequences aa and bb are different if there is some position ii such that aibia_i \neq b_i .

Let's call a string zz chaotic if for all ii from 22 to z|z| zi1ziz_{i-1} \neq z_i .

Let s[l,r]s[l,r] for some 1lrs1 \le l \le r \le |s| be a substring of consecutive letters of ss , starting from position ll and ending at position rr inclusive.

Let f(l1,r1,l2,r2)f(l_1, r_1, l_2, r_2) be the number of different merging sequences of x[l1,r1]x[l_1,r_1] and y[l2,r2]y[l_2,r_2] that produce chaotic merges. Note that only non-empty substrings of xx and yy are considered.

Calculate 1l1r1x1l2r2yf(l1,r1,l2,r2)\sum \limits_{1 \le l_1 \le r_1 \le |x| \\ 1 \le l_2 \le r_2 \le |y|} f(l_1, r_1, l_2, r_2) . Output the answer modulo 998244353998\,244\,353 .

输入格式

The first line contains a string xx ( 1x10001 \le |x| \le 1000 ).

The second line contains a string yy ( 1y10001 \le |y| \le 1000 ).

Both strings consist only of lowercase Latin letters.

输出格式

Print a single integer — the sum of f(l1,r1,l2,r2)f(l_1, r_1, l_2, r_2) over 1l1r1x1 \le l_1 \le r_1 \le |x| and 1l2r2y1 \le l_2 \le r_2 \le |y| modulo 998244353998\,244\,353 .

输入输出样例

  • 输入#1

    aaa
    bb

    输出#1

    24
  • 输入#2

    code
    forces

    输出#2

    1574
  • 输入#3

    aaaaa
    aaa

    输出#3

    0
  • 输入#4

    justamassivetesttocheck
    howwellyouhandlemodulooperations

    输出#4

    667387032

说明/提示

In the first example there are:

  • 66 pairs of substrings "a" and "b", each with valid merging sequences "01" and "10";
  • 33 pairs of substrings "a" and "bb", each with a valid merging sequence "101";
  • 44 pairs of substrings "aa" and "b", each with a valid merging sequence "010";
  • 22 pairs of substrings "aa" and "bb", each with valid merging sequences "0101" and "1010";
  • 22 pairs of substrings "aaa" and "b", each with no valid merging sequences;
  • 11 pair of substrings "aaa" and "bb" with a valid merging sequence "01010";

Thus, the answer is 62+31+41+22+20+11=246 \cdot 2 + 3 \cdot 1 + 4 \cdot 1 + 2 \cdot 2 + 2 \cdot 0 + 1 \cdot 1 = 24 .

首页