Mars
Time limit2sMemory limit512 MB
For each query substring, find the minimum number of bit flips that make it match no substring of the DNA, or report Impossible.
- Level
Hard8 of 10
- Topics
- String matching, Dynamic programming, String
- Solved
- No attempts yet
Problem
A new form of life was found on Mars. Every alien has a DNA that uses an alphabet of two letters instead of four, so the DNA of a Mars alien is written as a binary string.
Let be an alien DNA of length . The DNA has marked regions called genes. The gene at is the substring of the DNA that runs from position to position , both included (). Genes may overlap, and one gene may sit inside another.
During the life of a Mars alien each gene is copied billions of times. A protein binds to the start of the gene and copies it from start to end. This process is not error free and may produce mutations. In one mutation a 0 in the gene is copied as 1, or a 1 in the gene is copied as 0. A mutated copy no longer matches the gene, but it may still match a substring at another position of the DNA, and that substring may overlap the gene.
For example, let be 001011111 with a gene at , so the gene string is 1011. One copy of this gene is 1111, mutated at the second letter. That copy does not match the substring at , but it matches the substring at . A mutated copy is called degenerate if it occurs at no position of the whole DNA. The copy 1010, which mutates the fourth letter, is degenerate, but 1111 is not.
For each gene, find the smallest number of mutations that can produce a degenerate copy of that gene.
Input
The input has several test cases. The first line of a test case has two integers and (, ). The next line has a binary string of length . Each of the next lines has the location of one gene as two space separated integers and (). The input ends with a line holding 0 0, which you do not process.
Output
For each gene, print the smallest number of mutations that can produce a degenerate copy. If no set of mutations applied to that gene can produce a degenerate copy, print Impossible instead.