Codeforces Round #344 (Div. 2)D. Messenger【kmp】
来源:互联网 发布:淘宝10元包邮怎么赚钱 编辑:程序博客网 时间:2024/05/24 02:27
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 nblocks, 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 sin 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 , , ...
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.
Print a single integer — the number of occurrences of s in t.
5 33-a 2-b 4-c 3-a 2-c2-a 2-b 1-c
1
6 13-a 6-b 7-a 4-c 8-e 2-a3-a
6
5 51-h 1-e 1-l 1-l 1-o1-w 1-o 1-r 1-l 1-d
0
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.
题意:给出两个字符串所给的形式为 每个字符串的每段的字符和它的长度 求子串在主串中出现的次数
解题思路:去掉子串的头和尾后kmp计算中间部分在主串中出现的次数最后判断即可
/* ***********************************************Author : rycCreated Time : 2016-08-16 TuesdayFile Name : E:\acm\codeforces\344D.cppLanguage : c++Copyright 2016 ryc All Rights Reserved************************************************ */#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<list>#include<queue>#include<vector>#include<map>#include<stack>using namespace std;typedef long long LL;typedef pair<int,int>pii;const int maxn=200010;struct Node{ long long l; char c;}S[maxn],t[maxn],s[maxn];int Next[maxn];void getNext(int n){ int i=0,j=-1;Next[i]=j; while(i<n-1){ if(j==-1||(s[i+1].c==s[j+1].c&&s[i+1].l==s[j+1].l)){ i++;j++; Next[i]=j; } else j=Next[j]; }}LL KMP(int n,int m){ LL ans=0;int i=1,j=0; while(i<n-1){ if(j==-1||(t[i].c==s[j+1].c&&t[i].l==s[j+1].l))++i,++j; else { j=Next[j]; } if(j+1==m-1){ if(t[i-m+1].c==s[0].c&&t[i].c==s[m-1].c&&t[i-m+1].l>=s[0].l&&t[i].l>=s[m-1].l)ans++; } } return ans;}int main(){ int n,m,cntn=0,cntm=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;++i){ scanf("%lld-%c",&t[cntn].l,&t[cntn].c); if(cntn&&t[cntn].c==t[cntn-1].c){ t[cntn-1].l+=t[cntn].l; } else { cntn++; } } for(int i=1;i<=m;++i){ scanf("%lld-%c",&s[cntm].l,&s[cntm].c); if(cntm&&s[cntm].c==s[cntm-1].c){ s[cntm-1].l+=s[cntm].l; } else { cntm++; } } LL ans=0; if(cntm==1){ for(int i=0;i<cntn;i++){ if(s[0].c==t[i].c&&s[0].l<=t[i].l)ans+=t[i].l-s[0].l+1ll; } } else if(cntm==2){ for(int i=0;i<cntn;i++){ if(s[0].c==t[i].c&&s[1].c==t[i+1].c&&s[0].l<=t[i].l&&s[1].l<=t[i+1].l) ans++; } } else { getNext(cntm); ans=KMP(cntn,cntm); } printf("%lld\n",ans); return 0;}
- Codeforces Round #344 (Div. 2) D. Messenger(kmp)
- Codeforces Round #344 (Div. 2)D. Messenger【kmp】
- Codeforces Round #344 (Div. 2) D. Messenger(kmp,细节)
- Codeforces Round #344 (Div. 2) D. Messenger (KMP)
- Codeforces Round #344 (Div. 2) D. Messenger
- Codeforces Round #344 (Div. 2) D. Messenger
- Codeforces Round #344 (Div. 2) D. Messenger CF631D
- Codeforces Round #269 (Div. 2) D kmp
- hdu 1711 && Codeforces Round #344 (Div. 2) D 【KMP】
- 【KMP】Codeforces Beta Round #93 (Div. 2 Only) D
- Codeforces Round #269 (Div. 2)D(KMP)
- Codeforces Round #117 (Div. 2)-D. Common Divisors(KMP)
- Codeforces Round #299 (Div. 2)-D. Tavas and Malekas(kmp)
- Codeforces 631D:Messenger KMP
- Codeforces 631D Messenger【KMP】
- Codeforces 631D - Messenger KMP
- [KMP] Codeforces #631D. Messenger
- Codeforces Round #344 (Div. 2) 乱搞+单调栈+kmp
- UVA 116 Unidirectional TSP 动态规划+类似数塔
- 338. Counting Bits
- About Core Animation
- ---使用HttpSession防止表单重复提交
- DMA
- Codeforces Round #344 (Div. 2)D. Messenger【kmp】
- Android自定义控件之日历控件
- sql create table index
- SpringMVC 上传文件出现 Provisional headers are shown 和 response 数据 无法输出问题
- node之hello world
- 工厂,模式
- 总结下这周的get点w4day3
- 返回可用的内存空间 和 返回总的内存空间
- C++数据结构 之 二叉搜索树_Binary Search Tree