luogu#P5231. [JSOI2012] 玄武密码
[JSOI2012] 玄武密码
Background
By the beautiful Xuanwu Lake, beside Jiming Temple and in front of Jilong Mountain, there is a rich and beautiful land called Jinxiang River. It is said that one day, a trace of purple aura came down from the sky, and in an instant it disappeared into the Jinxiang River. The elders said that this was the Xuanwu spirit hiding the heavenly book here.
Many years later, people finally found writings with the Xuanwu cipher in the Jinxiang River area. Even more amazingly, these writings with the Xuanwu cipher have a subtle connection with the structure of Taicheng on the south bank of Xuanwu Lake. Thus, the long work of deciphering began.
Problem 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