UVa 1362 Exploring Pyramids: Multi-fork Tree traversal &DP

Source: Internet
Author: User

1362-exploring Pyramids

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_ problem&problem=4108

Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryption of ancient hieroglyphs on the walls nearby showed this caves structure is as follows. There are N caves in a pyramid, connected by narrow passages, one of the caves was connected by a passage to the outer worl D. The system of the "passages is" organized in such a way, that there is exactly one way to get from outside to each cave a Long passages. All caves are located in the basement of the pyramid, so we can consider them being located in the same. Passages do not intersect. Each cave has it walls colored in one of several various.

The scientists have decided to create a more detailed description of the caves, so they decided to use a exploring Rob Ot. The robot they are planning to use has two of types of output memory-the, tape are which for used down the writing On the caves, and the operating memory organized as a stack.

The

The robot enters the cave connected to the outer world along the passage. When it is travels along any passage for the "the", it puts its description in the top of it stack. When the robot enters any cave, it prints the "color of its" walls to its output tape. After this it chooses the leftmost passage among those it has not yet travelled and goes it. If There is no such passage, the robot takes the passage description it stack and travels along it Reverse direction. The robot ' s task is over when it returns to the outside of the pyramid. It is easy to during It trips the robot visits each cave at least once and travels along each passage exactly E in each direction.

The scientists have sent the robot to its mission. After it returned they started to study the output tape. What a great disappointment they have had after they have understood this output tape does not describe the cave syste M uniquely. Now they have a new problem-they want to know how many different cave systems could have the output produced tape h Ave. help them-find.

Since the requested number can be quite large and you should output it modulo 1 000 000 000. Please, this is the absolute locations of the caves are not important, but their relative locations-are important, so th E Caves (c) and (d) on the picture below are considered different.

Input

The input file contains several test cases, and each of them consists of a single line with the output tape that the Archa Eologists have. The output tape is the sequence of colors of caves in order the robot visited. The colors are denoted by capital letters of the 中文版 alphabet. The length of the tape does not exceed-characters.

Output

For each input case, write to the output a single line containing one integer number-the number of different cave system s (modulo 1) that could produce the output tape.

Sample Input

Abababa 
AB

Sample Output

5 
0

DP (I,J) =SUM{DP (i+1,k-1) *DP (k,j) | i+2<=k<=j,si=sk+sj}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.