time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 (li, ci), where li is the length of the i-th block and ci 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 tptp + 1…tp + |s| - 1 = s, where ti 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 , , …
Input

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 “li-ci” (1 ≤ li ≤ 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 “li-ci” (1 ≤ li ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.
Output

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

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

Output

1

Input

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

Output

6

Input

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

Output

0

Note

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.

【题解】

以num个X字母的形式给出某个字符串t和s;
求s在t中的匹配个数;
s最后的长度最大为20W*100W….
不能直接用KMP。
只能在“压缩”的状态下搞;
这道题提供了这类问题的解法->依然是KMP;
只是在做KMP的时候要增加判断一下这个连续的区域块是不是全都是相同的;
然后做KMP的时候,匹配要从匹配串的第二位开始匹配;然后到倒数第二个;
这一段匹配之后再比较第一位和最后一位;
因为
aaabbbbccc
和abbbbc也是匹配的;
(压缩下的KMP);
注意开Long LongTAT

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define LL long longusing namespace std;const int MAXN = 3e5;int n,m,lent = 0,lens = 0;
LL tl[MAXN],sl[MAXN],fail[MAXN];
char tc[MAXN],sc[MAXN];const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
void input_LL(LL &r)
{r = 0;char t = getchar();while (!isdigit(t)) t = getchar();LL sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}void input_int(int &r)
{r = 0;char t = getchar();while (!isdigit(t)) t = getchar();int sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}int main()
{//freopen("F:\\rush.txt", "r", stdin);input_int(n);input_int(m);//...while (n--){int num;char key;scanf("%d-%c",&num,&key);if (lent>0 && tc[lent] == key)tl[lent]+=num;elsetc[++lent] = key,tl[lent]=num;}while (m--){int num;char key;scanf("%d-%c",&num,&key);if (lens>0 && sc[lens] == key)sl[lens]+=num;elsesc[++lens] = key,sl[lens]=num;}LL ans = 0;if (lens == 1){for (int i = 1;i <= lent;i++)if (tc[i] == sc[1])ans += max(0LL,tl[i]-sl[1]+1LL);}else{fail[2] = 2;fail[3] = 2;for (int i = 3;i <= lens;i++){int j = fail[i];while (j > 2 && (sc[j]!=sc[i] || sl[j]!=sl[i])) j = fail[j];fail[i+1] = (sc[j]==sc[i] && sl[j]==sl[i])?j+1:2;}int j = 2;for (int i = 1;i <=lent-1;i++){while (j > 2 && (sc[j]!=tc[i] || sl[j]!=tl[i])) j = fail[j];if (j <=lens-1 && (sc[j]==tc[i]&& sl[j]==tl[i])) j++;if (j==lens){if (sc[j] == tc[i+1] && sl[j] <= tl[i+1] && sc[1] == tc[i-lens+2] && sl[1] <= tl[i-lens+2])ans++;j = fail[j];}}}printf("%I64d\n",ans);return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7632114.html

【18.40%】【codeforces 631D】Messenger相关推荐

  1. 【Codeforces】【161Div2】

    [题目来源]http://www.codeforces.com/contest/263 [A. Beautiful Matrix] [解析]模拟即可.按照题目的意思,找到1所在的位置(x, y),然后 ...

  2. 【42.59%】【codeforces 602A】Two Bases

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  4. 【跃迁之路】【728天】程序员高效学习方法论探索系列(实验阶段485-2019.2.18)...

    实验说明 从2017.10.6起,开启这个系列,目标只有一个:探索新的学习方法,实现跃迁式成长 实验期2年(2017.10.06 - 2019.10.06) 我将以自己为实验对象. 我将开源我的学习方 ...

  5. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

  6. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

  7. 【codeforces 807C】Success Rate

    [题目链接]:http://codeforces.com/contest/807/problem/C [题意] 给你4个数字 x y p q 要求让你求最小的非负整数b; 使得 (x+a)/(y+b) ...

  8. 【codeforces 766E】Mahmoud and a xor trip

    [题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...

  9. 【codeforces 798A】Mike and palindrome

    [题目链接]:http://codeforces.com/contest/798/problem/A [题意] 让你严格改变一个字符,使得改变后的字符串为一个回文串; 让你输出可不可能; [题解] 直 ...

  10. 【37.50%】【codeforces 745B】Hongcow Solves A Puzzle

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

最新文章

  1. 源程序,解释器,编译器
  2. oracle 全局临时变量,如何解析Oracle PL / SQL中的简单XML片段并将其加载到全局临时表中?...
  3. MacOS下打包Python应用
  4. Algorithm:C++/python语言实现之求旋转数组最小值、求零子数组、求最长公共子序列和最长公共子串、求LCS与字符串编辑距离
  5. vs 常见问题汇总
  6. 13种重要的云原生工具,让交付过程更快
  7. hadoop--日志聚集功能的配置
  8. 更改oracle背景,Oracle 11gR2修改用户后导致系统HANG住
  9. IDEA两步删除版本控制
  10. 使用Tomcat Catalina进行Tomcat服务器虚拟目录设置
  11. 使用FileTypesMan修复office关联图标
  12. 大型稀疏矩阵计算的现代方法介绍
  13. 2004古墓丽影黄金关卡——Lara在电影中:一号门
  14. 中国电子与IBM携手构建健康云平台;微软推3款机器学习工具;【软件网每日新闻播报│第9-26期】
  15. 董明珠的“接班人”出现了!这个22岁小姑娘,凭什么?
  16. lua 自实现pairs
  17. 如何用python画七彩蟒蛇_Python实现七彩蟒蛇绘制实例代码
  18. 从MUD到MMO——虚拟世界发展简史以及未来可能性的杂谈
  19. UG快捷键使用技巧总结(补充中....)
  20. 前端:移动端“淘宝造物节” 3D绚酷空间 VR 场景

热门文章

  1. 每天一点正则表达式积累(五)
  2. java高并发代码示例,Java使用代码模拟高并发操作的示例
  3. 为什么大家都说 SELECT * 效率低
  4. mac 卸载ssh 重新安装mysql,linux下彻底卸载MySQL
  5. 多功能数字钟c语言单片机PPT,单片机多功能数字时钟设计电路大全(五款单片机多功能数字时钟设计电路)...
  6. python安装cvxopt_python如何安装cvxopt
  7. python-gui-pyqt5的使用方法-5--为类增加信号
  8. scrapy[skp]快速入门
  9. nginx 超时设置_Nginx最详细的反向代理配置步骤,拿去不谢
  10. SpringBoot在前端发送url时,不能识别特殊字符的问题