I haven't written KMP for a long time.
It's totally unexpected.
First, merge the same continuum.
Assuming that the number of s characters is 1, it is legal as long as the length is sufficient and the characters are the same.
Assuming that the number of s characters is 2, the length of both sides is enough.
But only one position is legal and cannot be moved left or right.
Assuming that the number of s characters >= 3, the first and last two lengths are sufficient and all of them are equal in the middle.
So get your own getfail and then match it with t
It is important to note that the length of the sentence is sufficient.
So we need to change the special judgment.
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <queue>
#include <cstdio>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <cmath>
#include <vector>
#include <ctime>
#include <bitset>
using namespace std;
#define pb push_back
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define ans() printf("%d",ans)
#define ansn() printf("%d\n",ans)
#define anss() printf("%d ",ans)
#define lans() printf("%lld",ans)
#define lanss() printf("%lld ",ans)
#define lansn() printf("%lld\n",ans)
#define fansn() printf("%.10f\n",ans)
#define r0(i,n) for(int i=0;i<(n);++i)
#define r1(i,e) for(int i=1;i<=e;++i)
#define rn(i,e) for(int i=e;i>=1;--i)
#define rsz(i,v) for(int i=0;i<(int)v.size();++i)
#define szz(x) ((int)x.size())
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define lowbit(a) (a&(-a))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mp make_pair
#define lrt rt<<1
#define rrt rt<<1|1
#define X first
#define Y second
#define PI (acos(-1.0))
#define sqr(a) ((a)*(a))
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1000000000+7;
const double eps=1e-9;
const int inf=0x3f3f3f3f;
const ll infl = 10000000000000000;
const int maxn= 200000+10;
const int maxm = 40000+10;
//Pretests passed
int in(int &ret)
{
char c;
int sgn ;
if(c=getchar(),c==EOF)return -1;
while(c!='-'&&(c<'0'||c>'9'))c=getchar();
sgn = (c=='-')?-1:1;
ret = (c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9')ret = ret*10+(c-'0');
ret *=sgn;
return 1;
}
struct node
{
ll l;
char ch;
bool operator ==(const node &o)
{
return l==o.l&&ch==o.ch;
}
bool operator <= (const node &o)
{
return l<=o.l&&ch==o.ch;
}
};
node t[maxn],s[maxn];
int n,m;
void init()
{
int j = 0;
for(int i=0;i<n;++i)
{
if(j==0||t[j-1].ch!=t[i].ch)t[j++] = t[i];
else t[j-1].l+=t[i].l;
}
n = j;
j = 0;
for(int i=0;i<m;++i)
{
if(j==0||s[j-1].ch!=s[i].ch)s[j++] = s[i];
else s[j-1].l+=s[i].l;
}
m = j;
}
int nt[maxn];
int main()
{
#ifdef LOCAL
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif // LOCAL
// int n,m;
sdd(n,m);
r0(i,n) scanf("%lld-%c",&t[i].l,&t[i].ch);
r0(i,m) scanf("%lld-%c",&s[i].l,&s[i].ch);
init();
ll ans = 0;
if(m==1)
{
r0(i,n)
{
if(s[0]<=t[i])ans+=t[i].l-s[0].l+1;
}
}
else if(m==2)
{
r0(i,n-1)
{
if(s[0]<=t[i]&&s[1]<=t[i+1])++ans;
}
}
else
{
nt[1] = 0;
for(int i = 2 ; i < m - 1 ; ++i)
{
int j = nt[i-1];
while(j&&! (s[j+1]==s[i]) )j = nt[j];
if(s[j+1]==s[i])++j;
nt[i] = j;
}
for(int i = 1, j = 0 ; i<n-1;++i)
{
while(j&&!(s[j+1]==t[i]))j = nt[j];
if(s[j+1]==t[i])++j;
if(j==m-2)
{
if(s[0]<=t[i-j]&&s[j+1]<=t[i+1])++ans;
j = nt[j];
}
}
}
lansn();
return 0;
}