Period
Time limit1sMemory limit128 MB
Split string x into pieces to minimize the largest edit distance between y and any piece.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
Given two strings and over an alphabet , the edit distance between and is the minimum number of edit operations needed to turn into . There are three edit operations:
- change: replace one character of with a single character of .
- deletion: delete one character from .
- insertion: insert one character of into .
For example, the figure below shows that the edit distance between and is : a change (replacing b with h), a deletion (deleting d), and an insertion (inserting i).

The exact period of a repetitive string is defined as follows. A string is the exact period of a string if can be written as with , where is the shortest such string. For example, if then , so is the exact period of .
An approximate period is defined similarly. Given strings and , suppose is split into non-empty substrings so that . If the edit distance between and every substring is at most an integer , then is called a -approximate period of .
Given and , find the minimum such that is a -approximate period of . For example, if and , then can be split as , and the edit distances between and abcd, abc, abb are , , and . The largest of these is , so is a -approximate period of and the minimum is .
Input
The input is read from standard input. The first line contains the number of test cases . Each test case is given on two lines: the first line contains the string and the second line contains the string . The length of satisfies and the length of satisfies . Both strings consist only of lowercase English letters (the alphabet ).
Output
Write to standard output. For each test case, print exactly one line containing the minimum integer such that is a -approximate period of .