DNA Copy
Time limit1sMemory limit128 MB
Given a source string S of length at most 18, find the minimum number of copy operations (each taking a contiguous substring of S or of the already-built target, optionally reversed) needed to assemble T.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation, String, Brute force
- Solved
- No attempts yet
Problem
Sang-geun decided to simulate DNA copying for his graduation project.
A DNA string consists only of the letters A, C, G, and T. Given a source string , find the minimum number of copy operations needed to build a target string .
Each copy operation follows these rules:
- The copied piece must be a contiguous part of , or a contiguous part of that has already been built.
- The piece may be reversed before it is placed.
- Each copied piece fills one contiguous segment of ; once every segment is filled, is complete.
In other words, every position of the finished is filled by exactly one copy, taken either from or from an already-built part (reversal allowed). The order of filling is free, but a part used as a source must already be complete at the moment it is copied.
For example, with ACTG and GTACAATTAAT, can be built in copies:
- Copy
TGfrom and reverse it toGT→GT......... - Copy
ACfrom →GTAC....... - Copy
TA, a part already built →GTAC...TA.. - Copy
TAand reverse it toAT→GTAC...TAAT - Copy
AAT, a part already built →GTACAATTAAT
Input
The first line contains the number of test cases (). Each test case consists of two lines: the first line contains the string and the second line contains the string . Both strings consist only of the letters A, C, G, and T, and their lengths are between and , inclusive.
Output
For each test case, print on its own line the minimum number of copies needed to build . If it is impossible, print impossible.