eolymp
bolt
Try our new interface for solving problems
Problems

Automatic Trading

Automatic Trading

Time limit 1 second
Memory limit 64 MiB

A brokerage firm is interested in detecting automatic trading. They believe that a particular algorithm repeats itself; that is, it makes the same sequence of trades at a later time. The firm has identified a set of 26 key stocks that they believe are likely to be traded in concert, and they’ve encoded a series of trades as a string of letters: the letter itself indicates the stock, upper case indicates a buy, lower case indicates a sell. They want you to write a program to determine, for any two starting points, the longest sequence of identical trades from those two starting points, starting from and including those starting points as the first trade in the sequence.

Input data

There will be several test cases in the input. Each test case will start with a string s on the first line, consisting solely of upper case and lower case letters (1length(s)100000). On the next line will be an integer q (1q100000), indicating the number of queries. The following q lines each describe a query with two integers, i and j (0i < j < length(s)), which represent two zero-based positions in the string. The input will end with a line containing only an asterisk ('*').

Output data

For each query, output a single integer, indicating the length of the longest sequence of trades starting at i which is identical to the sequence of trades starting at j. Output no spaces. Do not print any blank lines between lines of output.

Source The University of Chicago Invitational Programming Contest 2013, March 29-31, 2013