CF631D.Messenger

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve.

All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of n blocks, each block containing only equal characters. One block may be described as a pair (l__i, c__i), where l__i is the length of the i-th block and c__i is the corresponding letter. Thus, the string s may be written as the sequence of pairs .

Your task is to write the program, that given two compressed string t and s finds all occurrences of s in t. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that p is the starting position of some occurrence of s in t if and only if t__p__t__p + 1...t__p + |s| - 1 = s, where t__i is the i-th character of string t.

Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as , , ...

“Blake科技”公司的每位员工都使用一款名为“Blake信使”的专用即时通讯应用。所有员工都非常喜欢这款应用,并持续使用它。然而,该应用缺少一些重要功能。例如,许多用户希望能够搜索消息历史记录。官方已宣布,这一新功能将在最近一次更新中上线,但开发人员却遇到了一些难题,而只有你能帮助他们解决。

所有消息均表示为仅由小写英文字母组成的字符串。为减轻网络负载,这些字符串以一种特殊的压缩形式存储。压缩算法的工作方式如下:字符串被表示为 $ n $ 个块的拼接,每个块仅包含相同的字符。每个块可描述为一个二元组 $ (l_i, c_i) $,其中 $ l_i $ 表示第 $ i $ 个块的长度,$ c_i $ 表示对应的字母。因此,字符串 $ s $ 可表示为如下序列的二元组:

你的任务是编写一个程序:给定两个压缩字符串 $ t $ 和 $ s $,找出 $ s $ 在 $ t $ 中的所有出现位置。开发人员知道这样的出现位置可能非常多,因此他们只要求你计算其总数。注意:当且仅当 $ t_p t_{p+1} \dots t_{p+|s|-1} = s $ 时,位置 $ p $ 才是 $ s $ 在 $ t $ 中某次出现的起始位置,其中 $ t_i $ 表示字符串 $ t $ 的第 $ i $ 个字符。

注意:字符串的压缩表示方式未必唯一。例如,字符串 “aaaa” 可表示为
、
、
……

输入格式

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of blocks in the strings t and s, respectively.

The second line contains the descriptions of n parts of string t in the format "l__i-c__i" (1 ≤ l__i ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.

The second line contains the descriptions of m parts of string s in the format "l__i-c__i" (1 ≤ l__i ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.

输入的第一行包含两个整数 nn 和 mm(1 ≤ n, m ≤ 200 0001 ≤ n, m ≤ 200\,000),分别表示字符串 tt 和 ss 中的段数。

第二行包含对字符串 tt 的 nn 段的描述,格式为“lil_i-cic_i”(1 ≤ li ≤ 1 000 0001 ≤ l_i ≤ 1\,000\,000)——其中 lil_i 表示第 ii 段的长度,cic_i 为其对应的英文小写字母。

第三行包含对字符串 ss 的 mm 段的描述,格式为“lil_i-cic_i”(1 ≤ li ≤ 1 000 0001 ≤ l_i ≤ 1\,000\,000)——其中 lil_i 表示第 ii 段的长度,cic_i 为其对应的英文小写字母。

输出格式

Print a single integer — the number of occurrences of s in t.

输出一个整数——字符串 ss 在字符串 tt 中出现的次数。

输入输出样例

  • 输入#1

    5 3
    3-a 2-b 4-c 3-a 2-c
    2-a 2-b 1-c

    输出#1

    1
  • 输入#2

    6 1
    3-a 6-b 7-a 4-c 8-e 2-a
    3-a

    输出#2

    6
  • 输入#3

    5 5
    1-h 1-e 1-l 1-l 1-o
    1-w 1-o 1-r 1-l 1-d

    输出#3

    0

说明/提示

In the first sample, t = "aaabbccccaaacc", and string s = "aabbc". The only occurrence of string s in string t starts at position p = 2.

In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and s = "aaa". The occurrences of s in t start at positions p = 1, p = 10, p = 11, p = 12, p = 13 and p = 14.

在第一个样例中,t=“aaabbccccaaacc”t = \text{``aaabbccccaaacc''},字符串 s=“aabbc”s = \text{``aabbc''}。字符串 ss 在字符串 tt 中的唯一一次出现起始于位置 p=2p = 2。

在第二个样例中,t=“aaabbbbbbaaaaaaacccceeeeeeeeaa”t = \text{``aaabbbbbbaaaaaaacccceeeeeeeeaa''},且 s=“aaa”s = \text{``aaa''}。ss 在 tt 中的出现起始位置为 p=1p = 1、p=10p = 10、p=11p = 11、p=12p = 12、p=13p = 13 和 p=14p = 14。

输入解题思路,AI测评打分。不知道怎么写?

首页