首页 > 代码库 > 2014牡丹江网络预选赛I题(字符串hash+简单DP)zoj3817

2014牡丹江网络预选赛I题(字符串hash+简单DP)zoj3817

Chinese Knot

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

Chinese knot is a decorative handicraft that began as a form of Chinese folk artifact in the Tang and Song Dynasty in China. It was later popularized in the Ming.

Alice is learning Chinese knotting. To take the first step, she went to the store and bought a string. To her surprise, she found the string was composed of characters! Anyway, she made a simple Chinese knot. It consists of one knot with 4 loops:

Look at the picture above. There are four loops connected at the center black knot. To make the image clear and neat, the characters on the string are shown as red and yellow points. As a Mid-Autumn Festival gift, Alice give this handcrafted Chinese knot to you. Then she asked you a question:

You are given a target character sequence. Can you get the sequence by walking on the knot? "Walking on the knot" means:

  • You can start at any point you want.
  • You can not start at or move to the black knot in the center.
  • Each step, you can only move to an adjacent point, except for the point you came from immediately.
  • The 8 points closely around the black knot are adjacent to each other. For example, in the picture above, they are the 8 yellow points adjacent to the black knot.
  • Your path should not contain three continuous points which are both one of the 8 points mentioned above.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N and M (3 <= NM <= 100000). The total length of string that Alice bought from store is 4 * N.

Then followed by 4 lines, each line contains N lower case letters as a loop of the Chinese knot. These characters are indexed from 1 to 4 * N corresponding to the input order.

The next line contains M lower case letters indicating the target character sequence.

Output

For each test case, output the M indexes of characters you visited. If there are multiple solutions, output any one. If there is no solution, output "No solution!" instead.

Sample Input

2
3 3
abc
abc
abc
abc
baa
3 3
abc
abc
abc
abc
bab

Sample Output

2 1 4
No solution!

Hint

In the first case, we start at the second character of the first loop and stop at the first character of the second loop.


题意:RT

思路:字符串hash

            将4个模式串正着反着构造出字符串hash,那么一共就是8个串了

            然后for一遍文本串,判断每个位置 i 是否可以以该位置的字符为开头将第 j 个模式串放进去,能放则qx[ i ][ j ]=true

            之后就是简单的DP了

            DP[i][j]表示由第 j 个模式串为结尾跑到了文本串的第 i 个字符

            转移:如果DP[i][j] 为真,那么在i+1个位置可以选择任意其它7个模式串继续跑,因为与第j个模式串为相反的不能选(不能再倒回去跑)

            因为题目还要求打印方案,所以DP数组可以存 放的模式串的长度,既可以起到判断可行,也可以起到最后打印方案的作用

            最后注意一下开头和结尾,特殊判断一下即可

<script src="https://code.csdn.net/snippets/466334.js" type="text/javascript"></script>

2014牡丹江网络预选赛I题(字符串hash+简单DP)zoj3817