"Test instructions" Ali like to collect all kinds of strange things, he recently Amoy to an old-fashioned typewriter. There are only 28 keys on the typewriter, with 26 lowercase English letters and ' B ' and ' P ' two letters printed respectively.
The ALI study found that the typewriter worked like this:
L Enter lowercase letters and the letter is added to a groove in the typewriter (the letter is added to the end of the groove).
L PRESS the ' B ' button and the last letter in the typewriter groove disappears.
L PRESS the ' P ' button and the typewriter will print all the existing letters in the groove and wrap them on the paper, but the letters in the groove will not disappear.
We numbered the printed string on the paper from 1 onwards to N. Typewriter has a very interesting function, in the typewriter hidden a small keyboard with a number, on the keypad input two number (x, y) (where 1≤x,y≤n), the typewriter will show the first x printed string in the first number of printed strings in the occurrence of how many times.
"Algorithm" AC automata + Tree array +dfs sequence
"Solving" first based on the operation sequence to build an AC automaton, "B" will return the jumping node, "P" is marked as a string end node.
Ask how many times a string A in the AC automaton appears in another string B?
From the root to walk the whole string A, the mark arrives at the node is 1, then is asked B in the tree of the fail the number of nodes is 1.
Ask that you can use a tree array to maintain DFS ordering for interval queries, which is part of the complexity O (n log n). How to modify the question string a?
The query is placed offline in the number order of string A (essentially sorted by DFS order), using a property "on each point into the stack +1, out of the stack-1, that each point to the root of the prefix and is the path to the root (Galaxy Exploration)."
So in order to enumerate the string A, add the new node +1, "B" backspace-1, these +1-1 are in the tree array of single-point modification, encountered an inquiry answered (equivalent prefix and). Because string A is a string in an AC automaton, there is no problem with fail.
The total complexity of O (n log n).
"Bzoj" 2434: [Noi2011] Ali typewriter ac automaton + tree-like array +dfs sequence