Master of copy and paste
InterviewTime limit2sMemory limit256 MB
Hanshin builds a target string by typing one character at a time or pasting a fixed clipboard string, and the task asks for the minimum total time.
- Level
Easy3 of 10
- Topics
- Greedy, String matching
- Solved
- No attempts yet
Problem
Hanshin types slowly, so he does almost everything at school with copy and paste. Typing a character by hand costs 1 second, so typing characters costs seconds. Pasting the clipboard costs 1 second no matter how long its contents are.
The clipboard holds the string , and its contents never change. Starting from an empty screen, Hanshin repeats two moves, typing a single character or pasting the whole of , until the screen shows exactly the string . Both moves append to the end of what he has built so far. With "bana" on the clipboard, building "banana" takes 3 seconds: paste "bana", then type 'n' and 'a'.
Find the minimum time needed to build .
Input
The first line contains the number of test cases ().
Each of the next lines contains two strings and separated by a single space. The length of is between 1 and 10,000, the length of is between 1 and 100, and both strings consist of lowercase English letters.
Output
For each test case, print on its own line the minimum time in whole seconds that Hanshin needs to build using .