Codeforces Round #344 (Div. 2) D. Messenger (KMP)
2016-09-07 23:52
274 查看
D. 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 ofn 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
output
input
output
input
output
Note
In the first sample, t = "aaabbccccaaacc",
and string s = "aabbc". The only occurrence of string s in
string t starts at positionp = 2.
In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa",
and s = "aaa". The occurrences of s in t start
at positionsp = 1, p = 10, p = 11, p = 12, p = 13 and p = 14.
题解:题意算了。一看了Note就会懂的。典型的KMP。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<set>
#include<stack>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)
#define in freopen("in.txt","r",stdin)
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
const int lowbit(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 1e9+7;
const ll inf =(1LL<<62) ;
const int MOD = 1e9+7;
const ll mod = (1LL<<32);
const int N =2e5+7;
const int M=100010;
const ll MAX=1e18;
//const int maxn=1001;
template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;}
template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}
int read(){
int v = 0, f = 1;char c =getchar();
while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}
while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
return v*f;}
int Next
;
int cntn=0,cntm=0;
struct Node{
ll l;
char c;
}S
,t
,s
;
void build1(int n)
{
for(int i=1;i<=n;++i)
{
scanf("%I64d-%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++;
}
}
return ;
}
void build2(int m)
{
for(int i=1;i<=m;++i)
{
scanf("%I64d-%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++;
}
}
return ;
}
void get_Next(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;
cin>>n>>m;
build1(n);
build2(m);
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
{
get_Next(cntm);
ans=KMP(cntn,cntm);
}
cout<<ans;
return 0;
}
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 ofn 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 positionp = 2.
In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa",
and s = "aaa". The occurrences of s in t start
at positionsp = 1, p = 10, p = 11, p = 12, p = 13 and p = 14.
题解:题意算了。一看了Note就会懂的。典型的KMP。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<set>
#include<stack>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)
#define in freopen("in.txt","r",stdin)
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
const int lowbit(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 1e9+7;
const ll inf =(1LL<<62) ;
const int MOD = 1e9+7;
const ll mod = (1LL<<32);
const int N =2e5+7;
const int M=100010;
const ll MAX=1e18;
//const int maxn=1001;
template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;}
template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}
int read(){
int v = 0, f = 1;char c =getchar();
while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}
while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
return v*f;}
int Next
;
int cntn=0,cntm=0;
struct Node{
ll l;
char c;
}S
,t
,s
;
void build1(int n)
{
for(int i=1;i<=n;++i)
{
scanf("%I64d-%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++;
}
}
return ;
}
void build2(int m)
{
for(int i=1;i<=m;++i)
{
scanf("%I64d-%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++;
}
}
return ;
}
void get_Next(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;
cin>>n>>m;
build1(n);
build2(m);
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
{
get_Next(cntm);
ans=KMP(cntn,cntm);
}
cout<<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 336 (Div 2) E】【KMP 构造 脑洞 好题 模拟找规律】Marbles 两条路径同时出发前后行走能否恰好同时到终点
- Codeforces Round #344 (Div. 2) D. Messenger
- Codeforces Round #170 (Div. 2)Tire--KMP
- Codeforces #269 (Div. 2)D. MUH and Cube Walls(KMP)
- Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
- codeforces #299 div2 D(Kmp || hash)
- CF 299 div2 D. Tavas and Malekas(KMP)
- Codeforces Round #269 (Div. 2) D题 MUH and Cube Walls(KMP)
- [KMP][字符串Hash] #93 div1 cf 126B Password
- Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)
- Codeforces Round #338 (Div. 2) C. Running Track(dp + kmp)
- Codeforces Round #269 (Div. 2)(D. MUH and Cube Walls(KMP))
- codeforces 93 div2 D题 - kmp- 3
- 【Codeforces Round 299 (Div 2)D】【KMP 本质是最前与最后匹配】Tavas and Malekas 长度为n的匹配串被模板串多位点覆盖的匹配串个数
- Codeforces #269 (Div. 2)D. MUH and Cube Walls(数据结构:KMP)
- Codeforces Beta Round #93 (Div. 1 Only)-B. Password(KMP)