【18.40%】【codeforces 631D】Messenger
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 long
using 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;
else
tc[++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;
else
sc[++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;
}
【18.40%】【codeforces 631D】Messenger的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【47.40%】【codeforces 743B】Chloe and the sequence
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【40.17%】【codeforces 569B】Inventory
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【27.40%】【codeforces 599D】Spongebob and Squares
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 761E】Dasha and Puzzle
[题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...
- 【26.83%】【Codeforces Round #380C】Road to Cinema
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【42.59%】【codeforces 602A】Two Bases
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- 【原创】面向对象版本地CPU资源占用监控脚本
前期准备: 1.python2.7环境 2.相关第三方库下载安装 脚本工作过程: 1.根据输入的进程名判断进程是否存在,如果不存在则进行等待,直到检测到进程PID,中途进程退出抛出异常,键入enter ...
- 鲁德http://www.testroad.org/topic/76
[最新的招聘信息]:1.联动优势科技有限公司招聘性能测试工程师 薪资20K左右 http://ask.testroad.org/article/4042.上海-交通银行总行-性能测试专家 ...
- SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能
SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能. 第一步:配置web.xml <!-- 配置Shiro过滤器,先让Shiro ...
- JAVA中正則表達式总结(具体解释及用途)
很多语言,包含Perl.PHP.Python.JavaScript和JScript,都支持用正則表達式处理文本,一些文本编辑器用正則表達式实现高级"搜索-替换"功能.所以JAVA语 ...
- js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写
js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写 一.总结 一句话总结:滚动事件scroll(),浏览器窗口调整监听resize(),思考好监听对象. 1.滚动事件scroll()的监听对象是 ...
- Socket编程模型之完毕port模型
转载请注明来源:viewmode=contents">http://blog.csdn.net/caoshiying?viewmode=contents 一.回想重叠IO模型 用完毕例 ...
- mac 系统 突破百度网盘网速限制
感谢https://blog.csdn.net/Deft_MKJing/article/details/82561564
- 使用perl读取Excel
使用perl读取Excel 环境 windows 7 ActiveState Perl Win32::OLE[perl package] 基本功能 循环处理多个sheet 读取Excel单元,提取in ...
- [CSS] Easily Reset Styles With a Single CSS value
There are times where you need to reset a an element’s styles. Instead of overwriting it with even m ...
- XHTML 结构化:使用 XHTML 重构网站 分类: C1_HTML/JS/JQUERY 2014-07-31 15:58 249人阅读 评论(0) 收藏
http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...