Codeforces Round #344 (Div. 2) D. Messenger (KMP)
D. Messenger
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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
inputCopy
5 3
3-a 2-b 4-c 3-a 2-c
2-a 2-b 1-c
outputCopy
1
inputCopy
6 1
3-a 6-b 7-a 4-c 8-e 2-a
3-a
outputCopy
6
inputCopy
5 5
1-h 1-e 1-l 1-l 1-o
1-w 1-o 1-r 1-l 1-d
outputCopy
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.
题意:
就是找一个字符串在另一个字符串出现的次数。
思路:
我们在读入的时候把相邻的并且相等的字符合并在一起,然后当m<=2我们直接暴力来做,
当 m>=3使,我们去掉s字符串的第一个块和最后一个块,中间的部分建立next 双指标的数组,然后与t字符串进行双指标的kmp算法,当匹配到的时候,我们看匹配的前一个位置和s字符串的第一个块是否匹配,以及后一个位置和s字符串的最后一个块是否匹配,即是否字符相等,如果字符相等,个数是否大于等于。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 200010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
pair<ll,char> a[maxn];
pair<ll,char> b[maxn];
pair<ll,char> d[maxn];
int n,m;
ll ans=0ll;
int nextarr[maxn];
void build()
{
nextarr[0]=-1;
int i=0;
int j=-1;
while(i<m)
{
if(j==-1||(b[i].se==b[j].se&&b[i].fi==b[j].fi))
{
j++;
i++;
nextarr[i]=j;
}else
{
j=nextarr[j];
}
}
}
std::vector<pii> v;
void kmp()
{
int x=0,y=0;
while(x<n&&y<m)
{
if(a[x]==b[y])
{
x++;
y++;
}
else if(y==0)
{
x++;
}else
{
y=nextarr[y];
}
if(y==m)
{
v.push_back(mp(x-m,x+1));
y=nextarr[y];
}
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
scanf("%d %d",&n,&m);
ll temp;
char c;
repd(i,1,n)
{
scanf("%lld-%c",&temp,&c);
if(a[i-1].se==c)
{
a[i-1].fi+=temp;
i--;
n--;
}else
{
a[i].fi=temp;
a[i].se=c;
}
}
repd(i,1,m)
{
scanf("%lld-%c",&temp,&c);
if(b[i-1].se==c)
{
b[i-1].fi+=temp;
i--;
m--;
}else
{
b[i].fi=temp;
b[i].se=c;
}
}
if(m==1)
{
repd(i,1,n)
{
if(a[i].se==b[1].se&&a[i].fi>=b[1].fi)
{
ans+=a[i].fi-b[1].fi+1ll;
}
}
cout<<ans<<endl;
}else if(m==2)
{
repd(i,1,n)
{
if(a[i].se==b[1].se&&a[i].fi>=b[1].fi&&a[i+1].se==b[2].se&&a[1+i].fi>=b[2].fi)
{
ans++;
}
}
cout<<ans<<endl;
}else
{
repd(i,1,m)
{
d[i]=b[i];
}
repd(i,1,m-2)
{
b[i]=b[i+1];
}
m-=2;
repd(i,0,m-1)
{
b[i]=b[i+1];
}
repd(i,0,n-1)
{
a[i]=a[i+1];
}
build();
kmp();
for(auto x:v)
{
// cout<<(a[x.fi-1].se==d[x.fi].se)<<" "<<(a[x.se-1].se==d[x.se].se)<<" "<<(a[x.fi-1].fi>=d[x.fi].fi)<<" "<<(a[x.se-1].fi>=d[x.se].fi)<<endl;
if(a[x.fi-1].se==d[1].se&&a[x.se-1].se==d[m+2].se&&a[x.fi-1].fi>=d[1].fi&&a[x.se-1].fi>=d[m+2].fi)
{
ans++;
}
// cout<<x.fi<<" "<<x.se<<endl;
}
cout<<ans<<endl;
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Codeforces Round #344 (Div. 2) D. Messenger (KMP)的更多相关文章
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #344 (Div. 2) D. Messenger kmp
D. Messenger 题目连接: http://www.codeforces.com/contest/631/problem/D Description Each employee of the ...
- Codeforces Round #262 (Div. 2) 460C. Present(二分)
题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...
- Codeforces Round #658 (Div. 2) D. Unmerge(dp)
题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...
- Codeforces Round #652 (Div. 2) C. RationalLee(贪心)
题目链接:https://codeforces.com/contest/1369/problem/C 题意 将 $n$ 个数分给 $k$ 个人,每个人分 $w_i$ 个数($\sum_{i = 1}^ ...
- Codeforces Round #284 (Div. 2) C题(计算几何)解题报告
题目地址 简要题意: 给出两个点的坐标,以及一些一般直线方程Ax+B+C=0的A.B.C,这些直线作为街道,求从一点走到另一点需要跨越的街道数.(两点都不在街道上) 思路分析: 从一点到另一点必须要跨 ...
- Codeforces Round #315 (Div. 2A) 569A Music (模拟)
题目:Click here 题意:(据说这个题的题意坑了不少人啊~~~)题目一共给了3个数---- T 表示歌曲的长度(s).S 表示下载了歌曲的S后开始第一次播放(也就是说S秒的歌曲是事先下载好的) ...
- Codeforces Round #260 (Div. 2)C. Boredom(dp)
C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #471 (Div. 2) F. Heaps(dp)
题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...
随机推荐
- IntelliJ IDEA 2019 注册码 (激活码) 有效期至2100年
IntelliJ IDEA 2019 注册码 (激活码) 有效期至2100年 本人使用的IDEA是最新版:IntelliJ IDEA 2018.3.3 x64 (IntelliJ IDEA官网下载地址 ...
- 有道自然语言翻译和文字识别OCR(图片文字识别)接口调用
官网 http://ai.youdao.com 文档地址 http://ai.youdao.com/docs/doc-ocr-api.s#p01 在Python中调用api. #/usr/bin/en ...
- C关于字符串操作的库函数实现总结
常用C关于字符串操作的库函数实现: //获取字符串长度 int Strlen(const char* s) { assert(s != NULL); ; while (*s++ != '\0') { ...
- Linux 概念与快捷方式
概念 何为shell Shell 是指"提供给使用者使用界面"的软件(命令解析器),类似于 DOS 下的 command(命令行)和后来的 cmd.exe .普通意义上的 Shel ...
- 【转贴】Windows virtio 驱动
Windows virtio 驱动 https://blog.51cto.com/dangzhiqiang/1833615 去年去中建总部的时候用过. 发现很多搞openstack的人都不清楚这一块的 ...
- 设计模式:门面模式(Facade)
前面介绍的适配器模式讲的是如何将一个接口转换成客户所需要的另一个接口,它的目的在于 解决接口的不兼容性问题.现在这里有这样一个模式,它的目的在于如何简化接口,它可以将多个类的复杂的一切隐藏在背后, ...
- 厉害了,ES 如何做到几十亿数据检索 3 秒返回!
一.前言 数据平台已迭代三个版本,从头开始遇到很多常见的难题,终于有片段时间整理一些已完善的文档,在此分享以供所需朋友的 实现参考,少走些弯路,在此篇幅中偏重于ES的优化,关于HBase,Hadoop ...
- CodeForce20C
这是一个裸的最短路的模板题,但是它需要输出路径. 用dijkstra的话首先敲一个最短路的板子,其次开一个数组p[]来记录路径,但是怎么存呢?我们需要记录每一个点的前驱,因为如果记录后边的话,一个点可 ...
- CF 1133B Preparation for International Women's Day
题目链接:http://codeforces.com/problemset/problem/1133/B 题目分析 读完题目,凡是先暴力.....(不用想,第四组数据就TLE了,QAQ) 当两个数的和 ...
- 双01字典树最小XOR(three arrays)--2019 Multi-University Training Contest 5(hdu杭电多校第5场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 题意: 给你两串数 a串,b串,让你一一配对XOR使得新的 C 串字典序最小. 思路: 首先这边 ...