Codeforces Round #344 (Div. 2) D. Messenger kmp
D. Messenger
题目连接:
http://www.codeforces.com/contest/631/problem/D
Description
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.
Sample Input
5 3
3-a 2-b 4-c 3-a 2-c
2-a 2-b 1-c
Sample Output
1
Hint
题意
给你两个字符串
每个字符串给你的形式是,有l1个c1在第一个位置,有l2个c2在第二个位置.....这样的
然后问你第一个字符串中有多少个和第二个字符串相同的子串
题解:
KMP就好了
这个和kmp其实差不多的,唯一不同的就是在开始位置和结束位置的时候,这两个位置的匹配只要第一个串这两个位置的字符大于等于第二个串的就好。
注意相邻的字符可能相同。
注意一种特殊情况
8 5
1-a 1-b 1-c 1-a 2-b 1-c 1-a 1-b
1-a 1-b 1-c 1-a 1-b
这种情况,就不能像平常kmp一样跳回到p[j]
于是我们就暴力的直接跳回到0就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+4;
vector<pair<long long,int> >V1,V2;
int n,m;
int p[maxn];
void kmp()
{
int j=0;
for(int i=1;i<V2.size();i++)
{
j=p[i];
while(j&&V2[j]!=V2[i])j=p[j];
if(V2[j]==V2[i])p[i+1]=j+1;
}
}
void get()
{
int j=0;
long long ans = 0;
for(int i=0;i<V1.size();i++)
{
if(j==V2.size()-1&&V2[j].second==V1[i].second&&V2[j].first<=V1[i].first)j++;
else{
while(j&&V2[j]!=V1[i])j=p[j];
if(j==0&&V2[j].second==V1[i].second&&V2[j].first<=V1[i].first)j++;
else if(V2[j]==V1[i])j++;
}
if(j==V2.size())
{
if(V1[i].first>V2[j-1].first)
ans++,j=0,i--;
else
ans++,j=p[j];
}
}
printf("%lld\n",ans);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
long long x;char y;
scanf("%lld",&x);
scanf("%c",&y);
scanf("%c",&y);
int z=y-'a'+1;
if(V1.size()&&V1.back().second==z)
V1.back().first+=x;
else
V1.push_back(make_pair(x,z));
}
for(int i=1;i<=m;i++)
{
long long x;char y;
scanf("%lld",&x);
scanf("%c",&y);
scanf("%c",&y);
int z=y-'a'+1;
if(V2.size()&&V2.back().second==z)
V2.back().first+=x;
else
V2.push_back(make_pair(x,z));
}
if(V2.size()==1)
{
long long ans = 0;
for(int i=0;i<V1.size();i++)
if(V1[i].second==V2[0].second&&V1[i].first>=V2[0].first)
ans+=(V1[i].first-V2[0].first+1);
printf("%lld\n",ans);
return 0;
}
kmp();
get();
return 0;
}
Codeforces Round #344 (Div. 2) D. Messenger kmp的更多相关文章
- Codeforces Round #344 (Div. 2) D. Messenger (KMP)
D. Messenger time limit per test2 seconds memory limit per test512 megabytes inputstandard input out ...
- Codeforces Round #344 (Div. 2)
水 A - Interview 注意是或不是异或 #include <bits/stdc++.h> int a[1005], b[1005]; int main() { int n; sc ...
- Codeforces Round #344 (Div. 2) A. Interview
//http://codeforces.com/contest/631/problem/Apackage codeforces344; import java.io.BufferedReader; i ...
- Codeforces Round #545 (Div. 2) D 贪心 + kmp
https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...
- Codeforces Round #344 (Div. 2) A
A. Interview time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳
E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...
- Codeforces Round #344 (Div. 2) C. Report 其他
C. Report 题目连接: http://www.codeforces.com/contest/631/problem/C Description Each month Blake gets th ...
- Codeforces Round #344 (Div. 2) B. Print Check 水题
B. Print Check 题目连接: http://www.codeforces.com/contest/631/problem/B Description Kris works in a lar ...
- Codeforces Round #344 (Div. 2) A. Interview 水题
A. Interview 题目连接: http://www.codeforces.com/contest/631/problem/A Description Blake is a CEO of a l ...
随机推荐
- upupw注入by pass
http:' and updatexml(null,concat(0x5c,(/*!00000select SCHEMA_name*/from/*!information_schema*/.schem ...
- Metlnfo cms后台getshell漏洞复现
整体思路 挖掘伪全局变量 然后找可控参数进行利用#伪全局变量:可理解为全局变量,例部分CMS为了全局过滤SQL注入或者XSS之类的漏洞就会将GET.POST.COOKIE等请求借入全局然后直接过滤.这 ...
- Linux 入门记录:五、vi、vim 编辑器
一.vi.vim编辑器 vi 是一个命令行界面下的文本编辑工具,最早在 1976 年由 Bill Joy 开发.当时名字叫做 ex.vi 支持绝大多数操作系统(最早在类 Unix 操作系统的 BSD上 ...
- ERROR: do not initialise statics to false
Question about git commit rule I git commit a patch, The patch has a "static int xxxxxxxxxxxxxx ...
- [hadoop][基本原理]zookeeper场景使用
代码:https://github.com/xufeng79x/ZkClientTest 1. 简介 zookeeper的特性决定他适用到某些场景非常合适,比如典型的应用场景: 1.集群管理(Grou ...
- [ python ] 项目:haproxy配置文件增删改查
1. 开发要求 实现对 haproxy.cfg 增删改查操作 2. 程序介绍 # 作者:hkey # 博客地址:https://www.cnblogs.com/hukey/p/9288279.html ...
- z-index失效的情况
1.父标签 position属性为relative: 2.问题标签无position属性(不包括static): 3.问题标签含有浮动(float)属性. 4.问题标签的祖先标签的z-index值比较 ...
- LeetCode218. The Skyline Problem
https://leetcode.com/problems/the-skyline-problem/description/ A city's skyline is the outer contour ...
- 机器学习方法:回归(一):线性回归Linear regression
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 开一个机器学习方法科普系列:做基础回顾之用,学而时习之:也拿出来与大家分享.数学水平有限,只求易懂,学习与工 ...
- Centos7使用squid实现正向代理
正向代理:代理服务器帮助客户端(浏览器)实现互联网的访问 (1)代理服务器配置 1.安装squid yum install squid -y 2.编辑squid配置文件 #vim /etc/squid ...