Document directory
- Problem description
- Input
- Output
- Sample Input
- Sample output
DNA-and-dnatime limit: 3000/1000 ms (Java/other) memory limit: 65535/32768 K (Java/other) total submission (s): 91 accepted submission (s): 45 Font: times New Roman | verdana | georgiafont size: Regular → Problem description the scientists found a strange DNA molecule, which can be viewed as a collection {, the element in B} is a series of n characters. Due to a series of mutations, the DNA chain only contains. Scientists found this strange, so they began to study more detailed genetic mutations. They discovered two types of genetic mutation. One type is to change the sequence of a single character (A → B or B → ). The second type changes the prefix of the entire sequence, from 1 to K (between 1 and N ). The smallest possible mutation in the number of computations can be the final state (only a character is included) that the starting molecule converts ). Mutations can occur in any order. The input has multiple groups of data. The first line of the input contains a positive integer N (1 ≤ n ≤ 1000 000), indicating the length of the molecule. Enter a string containing n characters in the second line. The string consists of A or B, indicating the starting state of the DNA molecule. The output line indicates the minimum number of required mutations. Sample Input
4ABBA5BBABB12AAABBBAAABBB
Sample output
224
I have to lament the magic of DP .. Let's get it done in two sentences .. O (n) time complexity
View code
# Include <stdio. h> # include <string. h> # include <stdlib. h ># include <iostream> using namespace STD; int N; int DP [1000100] [2]; /* DP [I] [0] indicates that the first I characters are all ADP [I] [1], indicating that the first I characters are all B */Char STR [1000100]; int main () {While (scanf ("% d", & N )! = EOF) {scanf ("% s", STR + 1); memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) {DP [I] [0] = min (DP [I-1] [0] + (STR [I] = 'B '), DP [I-1] [1] + 1 + (STR [I] = 'A ')); DP [I] [1] = min (DP [I-1] [0] + 1 + (STR [I] = 'B '), DP [I-1] [1] + (STR [I] = 'A');} printf ("% d \ n ", min (DP [N] [0], DP [N] [1] + 1);} return 0 ;}