Cousin Strings
Time limit1sMemory limit128 MB
Find the smallest n such that x is an n-th cousin of y, where each cousin step requires a common string reachable by deleting at most half of each string, or report that no n exists.
- Level
Medium7 of 10
- Topics
- Graph, BFS, String, Dynamic programming
- Solved
- No attempts yet
Problem
Two strings and are called first cousins if they can be made equal by deleting at most half of the characters from each string. For example, abcdef and axcyd are first cousins: deleting of the characters (b, e, f) from the first string and of the characters (x, y) from the second string leaves acd in both cases.
Two strings and are called -th cousins if there exists a string that is a first cousin of and an -th cousin of .
Given two strings and , find the smallest such that is an -th cousin of .
Input
The input contains several test cases. Each test case consists of two lines: the string on the first line and the string on the second line. Each of and contains between and lowercase letters. The input ends with a test case whose two lines each contain a single 0; this terminating case must not be processed.
Output
For each test case, print a single line containing the smallest for which is an -th cousin of , or not related if no such exists.