func_code_string: #include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   char s[maxn], t[maxn], st[maxn];   long long cnts[maxn], cntt[maxn];   long long Next[maxn];   int n, m;   long long ans;   long long lent, lens;   long long f, e;   void init() {     ans = 0;     lent = lens = 0;     s[0] = t[0] = 0;     char c[2];     long long 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;     long long j = 0;     for (long long 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;     }   }   long long kmp() {     long long sum = 0;     long long j = 0;     for (long long 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;   }   