#abc307h. Ex - Marquee

Ex - Marquee

Score : 600600 points

问题描述

存在一个长度为 LL 的字符串 SS,由大写和小写英文字母组成,该字符串显示在一个宽度为 WW 的电子公告板上。字符串 SS 从右向左以每步一个字符的宽度滚动。

显示重复一个包含 L+W1L+W-1 个状态的循环,在字符串 SS 的最后一个字符从左边消失时,字符串 SS 的第一个字符从右边边缘出现。

例如,当 W=5W=5S=S= ABC 时,公告板按顺序循环显示以下七个状态:

  • ABC..
  • BC...
  • C....
  • ....A
  • ...AB
  • ..ABC
  • .ABC.

. 表示没有字符显示的位置。)

更精确地说,在 k=0,,L+W2k=0,\ldots,L+W-2 时,有不相同的显示状态,其中:

  • f(x)f(x) 表示 xx 除以 L+W1L+W-1 的余数。公告板左侧第 (i+1)(i+1) 个位置显示的是:若 f(i+k)<Lf(i+k)<L,则显示 SS 的第 (f(i+k)+1)(f(i+k)+1) 个字符;否则不显示任何内容。

给你一个长度为 WW 的字符串 PP,它由大写英文字母、小写英文字母、._ 组成。
找出在公告板的 L+W1L+W-1 个状态中,除去 _ 位置外与 PP 相匹配的状态数目。
更精确地说,计算满足以下条件的状态数量。

  • 对于 i=1,,Wi=1,\ldots,W,满足以下任一条件:
    • 字符串 PP 的第 ii 个字符是 _
    • 公告板左侧第 ii 个位置显示的字符等于字符串 PP 的第 ii 个字符。
    • 公告板左侧第 ii 个位置没有显示任何内容,并且字符串 PP 的第 ii 个字符是 .

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a length LL string SS consisting of uppercase and lowercase English letters displayed on an electronic bulletin board with a width of WW. The string SS scrolls from right to left by a width of one character at a time.

The display repeats a cycle of L+W1L+W-1 states, with the first character of SS appearing from the right edge when the last character of SS disappears from the left edge.

For example, when W=5W=5 and S=S= ABC, the board displays the following seven states in a loop:

  • ABC..
  • BC...
  • C....
  • ....A
  • ...AB
  • ..ABC
  • .ABC.

(. represents a position where no character is displayed.)

More precisely, there are distinct states for k=0,,L+W2k=0,\ldots,L+W-2 in which the display is as follows.

  • Let f(x)f(x) be the remainder when xx is divided by L+W1L+W-1. The (i+1)(i+1)-th position from the left of the board displays the (f(i+k)+1)(f(i+k)+1)-th character of SS when f(i+k)<Lf(i+k)<L, and nothing otherwise.

You are given a length WW string PP consisting of uppercase English letters, lowercase English letters, ., and _.
Find the number of states among the L+W1L+W-1 states of the board that coincide PP except for the positions with _.
More precisely, find the number of states that satisfy the following condition.

  • For every i=1,,Wi=1,\ldots,W, one of the following holds.
    • The ii-th character of PP is _.
    • The character displayed at the ii-th position from the left of the board is equal to the ii-th character of PP.
    • Nothing is displayed at the ii-th position from the left of the board, and the ii-th character of PP is ..

Constraints

  • 1LW3×1051 \leq L \leq W \leq 3\times 10^5
  • LL and WW are integers.
  • SS is a string of length LL consisting of uppercase and lowercase English letters.
  • PP is a string of length WW consisting of uppercase and lowercase English letters, ., and _.

Input

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

LL WW

SS

PP

Output

Print the answer.

Sample Input 1

3 5
ABC
..___

Sample Output 1

3

There are three desired states, where the board displays ....A, ...AB, ..ABC.

Sample Input 2

11 15
abracadabra
__.._________ab

Sample Output 2

2

Sample Input 3

20 30
abaababbbabaabababba
__a____b_____a________________

Sample Output 3

2

Sample Input 4

1 1
a
_

Sample Output 4

1

update @ 2024/3/10 08:43:09