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
,
,
...
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 3 3-a 2-b 4-c 3-a 2-c 2-a 2-b 1-c
1
6 1 3-a 6-b 7-a 4-c 8-e 2-a 3-a
6
5 5 1-h 1-e 1-l 1-l 1-o 1-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.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
char s[maxn],t[maxn],st[maxn];
ll cnts[maxn],cntt[maxn];
ll Next[maxn];
int n,m;
ll ans;
ll lent,lens;
ll f,e;
void init(){
ans = 0;
lent = lens = 0;
s[0] = t[0] = 0;
char c[2];
ll x;
for (int i=1; i<=n; i++) {
scanf("%I64d-%s",&x,c);
if (c[0]==s[lens]) cnts[lens] +=x;
else {
cnts[++lens] = x;
s[lens] = c[0];
}
}
for (int i=1; i<=m; i++) {
scanf("%I64d-%s",&x,c);
if (c[0]==t[lent]) cntt[lent] +=x;
else {
cntt[++lent] = x;
t[lent] = c[0];
}
}
}
void getnxt(){
Next[0] = Next[1] = 0;
ll j = 0;
for (ll i=2; i<=lent-2; i++) {
while (j && (st[j+1]!=st[i] || cntt[i]!=cntt[j+1])) j = Next[j];
if (st[j+1] == st[i] && cntt[i] == cntt[j+1]) j++;
Next[i] = j;
}
}
ll kmp(){
ll sum = 0;
ll j = 0;
for (ll i=2; i<lens; i++) {
while (j && (st[j+1]!=s[i] || cnts[i]!=cntt[j+1])) j = Next[j];
if (st[j+1] == s[i] && cnts[i] == cntt[j+1]) j++;
if (j==lent-2){
if (s[i-(lent-2)]==t[1] && s[i+1]==t[lent] && cnts[i-(lent-2)]>=f && cnts[i+1]>=e) sum++;
j = Next[j];
}
}
return sum;
}
int main(){
while (scanf("%d %d",&n,&m)!=EOF){
init();
if (lent==1) {
for (int i=1; i<=lens; i++){
if (s[i]==t[lent] && cnts[i]>=cntt[lent])
ans += cnts[i] - cntt[lent]+1;
}
}
else if (lent==2) {
for (int i=1; i<lens; i++) {
if (s[i]==t[1] && s[i+1]==t[2] && cnts[i]>=cntt[1] && cnts[i+1]>=cntt[2]) ans++;
}
}
else {
strcpy(st+1,t+2);
st[lent] = 0;
f = cntt[1] , e = cntt[lent];
for (int i=1; i<lent; i++) cntt[i] = cntt[i+1];
getnxt();
ans = kmp();
}
printf("%I64d\n",ans);
}
return 0;
}
本文介绍了一种针对压缩字符串的高效匹配算法,该算法能够在不完全展开字符串的情况下找到所有模式串在目标串中的出现次数。文章详细解释了算法的工作原理,并通过示例说明了其应用场景。
326

被折叠的 条评论
为什么被折叠?



