#P5231. [JSOI2012] 玄武密码
[JSOI2012] 玄武密码
Description
After analysis, we can use the four directions east, south, west, and north to describe how the Taicheng bricks are arranged. Let a sequence of length describe it, where the elements are E, S, W, N, representing the four directions east, south, west, and north. We call this the main string.
The mysterious Xuanwu cipher is segments of text described by the patterns of the Four Symbols. The Four Symbols are the Azure Dragon of the east, the White Tiger of the west, the Vermilion Bird of the south, and the Xuanwu of the north, corresponding to the four directions east, south, west, and north.
Now the archaeologists have met a difficult problem. For each text segment , find its longest prefix such that is a substring of .
Input Format
The first line contains two integers, representing the length of the main string and the number of text segments.
The second line contains a string of length , representing the main string .
The next lines each contain a string, representing a text segment with the Xuanwu cipher.
Output Format
For each text segment, output one integer per line, representing the length of the longest .
7 3
SNNSSNS
NNSS
NNN
WSEE
4
2
0
Hint
Constraints
- For of the data, , .
- For of the data, , .
- For of the data, , .
- For of the data, , , , and both and contain only the letters
ESWN.
Translated by ChatGPT 5
京公网安备 11011102002149号