This page is still under construction.

Parts of this page are still being built. What you see may change.

Append

Time limit1sMemory limit128 MB

Summary
Given an LZ-style encoding as a list of (back-reference, length) pairs, count how many prefix positions split it into two valid non-empty encodings whose concatenation reproduces the original string.
Level

Hard8 of 10

Topics
String, Implementation, Brute force, Dynamic programming
Solved
No attempts yet

Problem

Consider the encoding scheme used by a well-known compression algorithm. We encode only sequences of lowercase letters. Such a sequence is encoded as a list of pairs (pi,ri)(p_i, r_i), where pi≥0p_i \ge 0 is an integer and rir_i is either a single character (when pi=0p_i = 0) or an integer with 0<ri≤pi0 < r_i \le p_i (when pi>0p_i > 0).

Decoding processes the pairs in order and builds the output sequence:

  • If pi=0p_i = 0, then rir_i is a character; append it to the end of the decoded sequence.
  • If pi>0p_i > 0, then rir_i is an integer with 0<ri≤pi0 < r_i \le p_i; append rir_i characters copied from the decoded sequence, starting pip_i positions before its current end.

For example, the pairs (0,a),(1,1),(0,b),(3,3),(3,3),(3,2),(0,c)(0, a), (1, 1), (0, b), (3, 3), (3, 3), (3, 2), (0, c) decode step by step: (0,a)(0, a) gives a; (1,1)(1, 1) gives aa; (0,b)(0, b) gives aab; (3,3)(3, 3) appends aab to give aabaab; the next (3,3)(3, 3) appends aab to give aabaabaab; (3,2)(3, 2) appends aa to give aabaabaabaa; and (0,c)(0, c) gives aabaabaabaac. Note that one string ww may have several different encodings.

For sequences uu and vv, write uvuv for their concatenation. Let CwC_w denote an encoding of a lowercase-letter sequence ww. Given an encoding CwC_w, count how many ways it can be split into two consecutive parts Cw=CuCvC_w = C_u C_v such that:

  • CuC_u is the prefix consisting of the first few pairs and CvC_v is the remaining suffix of pairs;
  • CuC_u is itself a valid encoding of some sequence uu, and CvC_v is itself a valid encoding of some sequence vv;
  • w=uvw = uv, and neither uu nor vv is empty.

Write a program that outputs this number.

Input

The input consists of several blocks of lines. Each block describes one encoding CwC_w. A line of a block contains either two integers pip_i and rir_i (with ri≤pi<1000r_i \le p_i < 1000) separated by a single space, or the integer 00 followed by a single space and a single lowercase letter. Each block is terminated by one empty line. The input may contain several such blocks, one after another.

Output

For each block, print a single line containing the number of ways the encoding CwC_w can be written as CuCvC_u C_v with w=uvw = uv, where both uu and vv are non-empty and each part is a valid encoding on its own.

Examples4

  1. Example 1

    Input
    0 a
    1 1
    0 b
    3 3
    3 3
    3 2
    0 c
    
    
    Expected output
    1
    
  2. Example 2

    Input
    0 a
    
    
    Expected output
    0
    
  3. Example 3

    Input
    0 a
    0 b
    
    
    Expected output
    1
    
  4. Example 4

    Input
    0 a
    0 b
    0 c
    
    
    Expected output
    2