eolymp
bolt
Try our new interface for solving problems
Problems

RNA Molecules

RNA Molecules

The RNA molecule is a sequence of four nucleotides \textbf{A}, \textbf{C}, \textbf{G}, and \textbf{U}. Due to the chemical structure of nucleotides, there could be hydrogen bonds established between any pair of \textbf{(A, U)}, \textbf{(U, A)}, \textbf{(G, C)} or \textbf{(C, G)} nucleotides, but not for the other cases (i.e. for instance \textbf{A} can’t make a hydrogen bond with either \textbf{C} or \textbf{G}). Every nucleotide can have hydrogen bond with at most one other nucleotide, and there could not be any hydrogen bond between two nucleotides that are adjacent in the RNA sequence as there is already a covalent interaction between them. In the living cells, RNA will fold and there would be many hydrogen bonds established. But the hydrogen bonds can’t cross each other, i.e. if there is a bond from nucleotide \textbf{i} to nucleotide \textbf{j} (\textbf{i} < \textbf{j}), the nucleotides \textbf{i+1}, … , \textbf{j−1} can have bonds only between themselves, and can’t have any bonds to either \textbf{1}, …, \textbf{i−1} or \textbf{j+1}, …, \textbf{n} where \textbf{n} is the number of nucleotides in the RNA. The real case for RNA is more complicated, but we are not going to face you so much complexities in this competition, making your life easier. Your task is to find the maximum possible bonds for a set of given RNA molecules. \InputFile There are multiple test cases in the input. Each test case starts with a line containing a non-negative integers \textbf{0} ≤ \textbf{n} ≤ \textbf{500}. The second line of each test case contains a string (RNA molecule) of size \textbf{n} consisting of characters \textbf{A}, \textbf{C}, \textbf{G}, and \textbf{U}. The input terminates with "\textbf{0}" which should not be processed. \OutputFile For each test case, output the maximum number of hydrogen bonds for the given RNA molecule.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
AU
4
AGCU
4
AGUC
0

Output example #1
0
1
1
Source 11th Iran Internet Programming Contest, 28 November, 2013 (7 Azar, 1392)