This page is still under construction.

Parts of this page are still being built. What you see may change.

Period

Time limit1sMemory limit128 MB

Summary
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 AA and BB over an alphabet Σ\Sigma, the edit distance between AA and BB is the minimum number of edit operations needed to turn AA into BB. There are three edit operations:

  • change: replace one character of AA with a single character of BB.
  • deletion: delete one character from AA.
  • insertion: insert one character of BB into AA.

For example, the figure below shows that the edit distance between A=abcdefgA = abcdefg and B=ahcefigB = ahcefig is 33: a change (replacing b with h), a deletion (deleting d), and an insertion (inserting i).

Edit distance example

The exact period of a repetitive string is defined as follows. A string pp is the exact period of a string xx if xx can be written as x=pkx = p^k with k≥1k \ge 1, where pp is the shortest such string. For example, if x=ababababx = abababab then x=(abababab)1=(abab)2=(ab)4x = (abababab)^1 = (abab)^2 = (ab)^4, so abab is the exact period of xx.

An approximate period is defined similarly. Given strings xx and yy, suppose xx is split into non-empty substrings p1,p2,…,ptp_1, p_2, \dots, p_t so that x=p1⋅p2⋯ptx = p_1 \cdot p_2 \cdots p_t. If the edit distance between yy and every substring pip_i is at most an integer kk, then yy is called a kk-approximate period of xx.

Given xx and yy, find the minimum kk such that yy is a kk-approximate period of xx. For example, if x=abcdabcabbx = abcdabcabb and y=abcy = abc, then xx can be split as x=p1⋅p2⋅p3=abcd⋅abc⋅abbx = p_1 \cdot p_2 \cdot p_3 = abcd \cdot abc \cdot abb, and the edit distances between y=abcy = abc and abcd, abc, abb are 11, 00, and 11. The largest of these is 11, so yy is a 11-approximate period of xx and the minimum kk is 11.

Input

The input is read from standard input. The first line contains the number of test cases TT. Each test case is given on two lines: the first line contains the string yy and the second line contains the string xx. The length of yy satisfies 1≤∣y∣≤501 \le |y| \le 50 and the length of xx satisfies 1≤∣x∣≤50001 \le |x| \le 5000. Both strings consist only of lowercase English letters (the alphabet Σ\Sigma).

Output

Write to standard output. For each test case, print exactly one line containing the minimum integer kk such that yy is a kk-approximate period of xx.

Examples4

  1. Example 1

    Input
    3
    abc
    abcdabcabb
    abab
    abababababab
    xyz
    abcdefghikjlmn
    
    Expected output
    1
    0
    3
    
  2. Example 2

    Input
    1
    abc
    abc
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    ab
    abababab
    
    Expected output
    0
    
  4. Example 4

    Input
    1
    ab
    ba
    
    Expected output
    1