编程介的小学生 2017-10-06 16:02 采纳率: 20.5%
浏览 917
已采纳

Append

Description

Consider the following encoding scheme used in one famous compresion algorithm. Suppose we will code only sequences of lower case letters. Each such sequence of characters can be encoded to a sequence of pairs (pi, ri), where pi >= 0 is an integer and ri is either a character (if pi = 0) or an integer greater than zero and less or equal than pi (if pi > 0).
We describe now the decoding procedure for our encoding scheme. Let (p1, r1), (p2, r2), be a code of a sequence. We get the sequence as follows: we take successively individual pairs of the code. If pi = 0 then ri is a character and we simply add ri to the end of already decoded sequence. If pi > 0 then ri is an integer, 0 < ri <= pi, and we add to already decoded sequence ri letters from this sequence starting at the position pi places before the end.

For example, consider the sequence of pairs (0 a), (1, 1), (0, b), (3, 3), (3, 3), (3, 2), (0, c). Decoding (0, a) we get a. Decoding (1, 1) we get aa. (0, b) adds b getting aab. (3, 3) will add aab, so now we have aabaab. Next pair (3, 3) will again add aab so we have aabaabaab. (3, 2) will add aa, so our sequence is aabaabaabaa and (0, c) adds c. So the decoded sequence is aabaabaabaac. Note that in general for a given w it can exist more such sequences of pairs.

Let u, v be some sequences. By uv we will understand the sequence created by appending of the sequence v to the end of sequence u. Let Cw be a sequence of pairs which encodes a sequence of lowercase letters w. Suppose we have given a sequence of pairs Cw. The question is how many possibilities does exist for expressing the sequence Cw in the form CuCv where u, v are sequences satisfying the equation w = uv and neither u nor v is empty. Write a program that will answer this question.
Input

The input consists of blocks of lines. Each block describes one sequence of pairs Cw to some w in such a way that the i-th line of the block contains either two integers pi, ri, (ri <= pi < 1000) separated by one space or 0 followed by one space and one character. Each block ends with one empty line.
Output

The output contains the lines corresponding to the blocks in the input. Each line contains the number of possibilities of representation of the sequence Cw in the form Cu Cv where u, v are sequences satisfying the equation w = uv and neither u nor v is empty.
Sample Input

0 a
1 1
0 b
3 3
3 3
3 2
0 c
Sample Output

1

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?