Palindromic DNA
Time limit3sMemory limit128 MB
Decide, given a cyclic DNA alphabet and palindrome constraints on many index subsets with a no-adjacent-change rule, whether a valid ±1/0 edit assignment exists.
- Level
Hard8 of 10
- Topics
- Union-find, Graph, Implementation, Math
- Solved
- No attempts yet
Problem
A DNA sequence is a string over four nucleobases — Adenine, Guanine, Thymine, and Cytosine — which we write using their initials A, G, T, and C. The bases have a cyclic order: A is followed by G, G by T, T by C, and C back to A.
Recent genomics research suggests that some diseases are linked to certain groups of bases failing to read as a palindrome. You are given a DNA string (indexed from ) together with subsets of positions , and you must transform so that, for every subset, the characters of at those positions read the same forwards and backwards. Formally, the restriction of to a subset with is the string , and every such restriction must be a palindrome.
You may inspect any base, but each base can be changed in only one of three ways:
- Leave it unchanged.
- Advance it by one step in the cyclic order (for example,
CbecomesA). - Move it back by one step (for example,
TbecomesG).
Because of a technological limitation, you may not change two bases that sit in consecutive positions of the sequence. Decide whether the goal is achievable.
For example, take the sequence AGTAT with subsets , , and . Advancing the first base and moving the last base back gives GGTAG; the three restrictions become GG, GG, and GTG, all palindromes, and the two changed positions ( and ) are not consecutive.
By contrast, the sequence CATGC with the two subsets and has no solution: positions , , and would all have to change to the same base (for example T), which forces the consecutive positions and to be modified together — and that is forbidden.
Input
The input contains several test cases. The first line of a test case has two integers and (, ): the length of the sequence and the number of subsets. The next line contains the DNA sequence, a string of length over the alphabet ACGT.
Each of the following lines describes one subset. A line begins with L:, where () is the number of positions in the subset, followed by the distinct positions in strictly increasing order, each between and . Subsets may overlap partially or completely.
A blank line separates consecutive test cases. The input ends with a line containing 0 0, which must not be processed.
Output
For each test case, print a single line containing YES if all the required restrictions can be made palindromic under the rules, or NO otherwise.