Automatic Trading
Time limit5sMemory limit128 MB
Given a string and pairs of positions, for each query find the length of the longest common prefix of the two suffixes starting at those positions.
- Level
Hard8 of 10
- Topics
- String, String matching, Binary search, Hash map
- Solved
- No attempts yet
Problem
A brokerage firm wants to detect automatic trading. They believe a particular algorithm repeats itself, making the same sequence of trades again at a later time. The firm has identified 26 key stocks that are likely to be traded in concert, and it has encoded a series of trades as a string of letters: the letter identifies the stock, an upper-case letter means a buy, and a lower-case letter means a sell.
For any two starting positions, determine the length of the longest run of identical trades that begins at each of the two positions, counting the trade at each starting position as the first trade of the run.
Input
There are several test cases. Each test case begins with a line containing a string made up solely of upper- and lower-case letters (). The next line contains an integer , the number of queries (). Each of the following lines describes one query with two integers and , two zero-based positions in the string ().
The input ends with a line containing only an asterisk (*).
Output
For each query, output a single integer: the length of the longest run of trades starting at position that is identical to the run of trades starting at position . Print no spaces, and print no blank lines between output lines.