Codeforces Round #344 (Div. 2) D. Messenger kmp
2016-03-04 20:22
260 查看
D. Messenger
题目连接:
http://www.codeforces.com/contest/631/problem/DDescription
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 33-a 2-b 4-c 3-a 2-c
2-a 2-b 1-c
Sample Output
1Hint
题意
给你两个字符串每个字符串给你的形式是,有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; }
相关文章推荐
- springMVC参数绑定
- POJ 2234 Matches Game
- 开启我的前端之路
- 用<canvas>写简单的图形
- HDU 5062 Beautiful Palindrome Number(暴力)
- 项目开发团队如何更有凝聚力
- 2.3 Bean装配
- 获取了职业资格证书二级/技师
- .bash_profile和.bashrc的区别(如何设置生效)
- Struts2传递参数的三种方式
- 密码学基本介绍
- Android的四种数据储存方式之SharedPreferences
- C++模板简介
- 第一章 【概论】练习与讨论 第二题
- 选一种你比较熟悉的软件,并点评它的优缺点,并描述此类软件的发展历史
- C语言_相关函数与结构体
- 011_用eclipse打开hadoop1.2.1源码出现The method getSplits(JobConf, int) is undefined for the type Object错误
- 使用CocoaPods 为项目配置第三方类库
- javaIO流之File类
- 在Linux中安装JDK步骤过程