CodeForces - 631D Messenger —— kmp思维

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lngxling/article/details/82146684

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 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

Copy

5 3
3-a 2-b 4-c 3-a 2-c
2-a 2-b 1-c

output

Copy

1

input

Copy

6 1
3-a 6-b 7-a 4-c 8-e 2-a
3-a

output

Copy

6

input

Copy

5 5
1-h 1-e 1-l 1-l 1-o
1-w 1-o 1-r 1-l 1-d

output

Copy

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 = 10, p = 11, p = 12, p = 13 and p = 14.

题意:

给出主串和子串,问子串在主串里出现过多少次

串都是一段一段以数字+字母的形式给出,表明有数字个字母

思路:

输入时不断把前后相邻的相同字符合并到一个里

如果子串只有一个字母或者两个字母,直接暴力匹配

有多个时,我们可以先拿掉第一个和最后一个字符,剩余的在主串中都必须字符和数字完全匹配,所以用剩余的部分在主串中跑KMP,每次遇到一个匹配了的地方就特判一下第一个和最后一个字符和数字

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll na[200010];
char a[200010];
ll nb[200010];
char b[200010];
ll f[200010];
int n,m;
bool check(int l,int r)
{
	if(a[l-1]==b[0]&&a[r+1]==b[m-1]&&na[l-1]>=nb[0]&&na[r+1]>=nb[m-1])
	return true;
	return false;
}
void kmp(char *a,char *b,int n,int m,ll *na,ll *nb,ll &ans)
{
	f[0]=-1;
	for(int i=1;i<m;i++)
	{
	    int j=f[i-1];
	    while ((b[j+1]!=b[i]||nb[j+1]!=nb[i])&&(j>=0))
	        j=f[j];
	    if (b[j+1]==b[i]&&nb[j+1]==nb[i])
	        f[i]=j+1;
	    else
	        f[i]=-1;
	}
	int i=0,j=0;
	while(i<n)
	{
	    if(a[i]==b[j]&&na[i]==nb[j])
	    {
	        i++;
	        j++;
	        if(j==m)
	        {
	            if(check(i-m+1,i))
	            ans++;
	            j=f[j-1]+1;
	        }
	    }
	    else
	    {
	        if(j==0)
	            i++;
	        else
	            j=f[j-1]+1;
	    }
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%lld-%c",&na[i],&a[i]);
		if(i>=1&&a[i]==a[i-1])
		{
			na[i-1]+=na[i];
			i--;
			n--;
		}
	}
	for(int i=0;i<m;i++)
	{
		scanf("%lld-%c",&nb[i],&b[i]);
		if(i>=1&&b[i]==b[i-1])
		{
			nb[i-1]+=nb[i];
			i--;
			m--;
		}
	}
	ll ans=0;
	if(m==1)
	{
		for(int i=0;i<n;i++)
		{
			if(a[i]==b[0]&&na[i]>=nb[0])
			{
				ans+=(ll)na[i]-nb[0]+1;
			}
		}
	}
	else if(m==2)
	{
		for(int i=1;i<n;i++)
		{
			if(a[i-1]==b[0]&&a[i]==b[1]&&na[i-1]>=nb[0]&&na[i]>=nb[1])
			{
				ans++;
			}
		}
	}
	else
	{
		kmp(a+1,b+1,n-2,m-2,na+1,nb+1,ans);
	}
	printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/Lngxling/article/details/82146684
今日推荐