Codeforces Round #344 (Div. 2) D. Messenger

本文详细解析了D.Messenger中消息的特殊压缩算法,并提出了解决搜索历史消息需求的策略。通过引入KMP算法的变形,有效解决了在压缩消息中查找特定子串的问题。详细步骤包括读取输入数据、处理字符串、应用KMP算法以及输出结果,最终实现了高效的消息搜索功能。
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 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 ...

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 position p = 2.

In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and s = "aaa". The occurrences of s in t start at positions p = 1,p = 10p = 11p = 12p = 13 and p = 14.

题意:

给你两个字符串每个字符串给你的形式是,有l1c1在第一个位置,有l2c2在第二个位置.....,问你第一个字符串中有多少个和第二个字符串相同的子串

 

思路: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

#include<bits/stdc++.h>
using namespace std;
const int maxn=201000;
int next[maxn];
struct node{
    char s[2];
    __int64 num;
}a[maxn],pattern[maxn];

void get_next(int len){
    int i=0,j=-1;
    next[0]=-1;
    while(i<len){
        if(j==-1||(pattern[i].s[0]==pattern[j].s[0]&&pattern[i].num==pattern[j].num)){
            i++,j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}

__int64 KMP(int n,int m){
    int i=0,j=0;
    __int64 num=0;
    while(i!=n){
        if(j==m-1){
            if(a[i].num<pattern[j].num||a[i].s[0]!=pattern[j].s[0])
                j=next[j];
            else
                j++;
        }
        else if(j==0){
            if(a[i].num>=pattern[j].num&&a[i].s[0]==pattern[j].s[0])
                i++,j++;
            else
                j=next[j];
        }
        else if(j==-1||(a[i].s[0]==pattern[j].s[0]&&a[i].num==pattern[j].num))
            i++,j++;
        else
            j=next[j];
        if(j==m){
            j=next[j]-1;
            num++;
        }
    }
    return num;
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    int cnt1=0,cnt2=0;
    for(int i=0;i<n;i++){
        scanf("%I64d-%s",&a[i].num,a[i].s);
        if(i<=0){
            a[cnt1].num=a[i].num;
            a[cnt1].s[0]=a[0].s[0];
            continue;
        }
        if(a[i].s[0]==a[i-1].s[0])
            a[cnt1].num+=a[i].num;
        else
            a[++cnt1].num=a[i].num,a[cnt1].s[0]=a[i].s[0];
    }
    for(int i=0;i<m;i++){
        scanf("%I64d-%s",&pattern[i].num,pattern[i].s);
        if(i<=0){
            pattern[cnt1].num=pattern[i].num;
            pattern[cnt1].s[0]=pattern[0].s[0];
            continue;
        }
        if(pattern[i].s[0]==pattern[i-1].s[0])
            pattern[cnt2].num+=pattern[i].num;
        else
            pattern[++cnt2].num=pattern[i].num,pattern[cnt2].s[0]=pattern[i].s[0];
    }
    cnt1++,cnt2++;
    get_next(cnt2);
    if(cnt2==1){
        __int64 ans=0;
        for(int i=0;i<cnt1;i++)
            if(a[i].s[0]==pattern[0].s[0]&&a[i].num>=pattern[0].num)
                ans=ans+(a[i].num-pattern[0].num+1);
        printf("%I64d\n",ans);
        return 0;
    }
    printf("%I64d\n",KMP(cnt1,cnt2));
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值