[Programming Questions] Diablo string
A string that contains only ' a ', ' B ', and ' C ', if there is one in a contiguous string of length 3 that is exactly ' a ', ' B ', and ' C ', then the string is pure, otherwise the string is Diablo. For example:
BAACAACCBAAA continuous substring "CBA" contains ' a ', ' B ', ' C ' each one, so is the pure string
Aabbccaabb does not exist a continuous substring of length 3 contains ' a ', ' B ', ' C ', so it is a dark string
Your task is to calculate a string of length n (containing only ' A ', ' B ' and ' C '), and how many are dark strings.
Input Description:
Enter an integer n representing the length of the string (1≤n≤30)
Output Description:
Outputs an integer that indicates how many dark strings are
Input Example:
23
Output Example:
921
Problem Solving Ideas:
It is also a kind of dynamic plan to find the law. It is also a typical dynamic programming of number theory.
Hypothesis: A string of length n. Pushed to by a string of the preceding n-1 length.
hypothesis : F (n-1) = S (N-1) + D (N-1); F: = n-1 long dark string number, S: = n1 Long black string results in the following two digits the same number D: The number of the latter two different; why so set, from N1 began to set the equation in order to pass F (N-1) to represent F (n)
Observe the change process: 1. S:eg:"AA"-1-bit result added: AAA AAB AAC2. D:eg:"AB"-1-bit results added: ABA ABBtargetf (n)=3*s (N-1) +2*d (N-1) =2*f (N-1) + S (N-1); Found S (n-1Don't know, we're just looking ahead and trying to figure out F (N-2) with S (N-1). Well, in the beginning, I didn't know who was in the relationship with other variables.
derivation: on the assumption that there is: F (n-1) = S (N-1) + D (N-1);3. Each s (N-1) can get 3 results, according to 1. One of them is the S state (S (n)) AAA4. Each d (N-1) can get 2 results, according to 2. One of them is the state of S (S (n)) ABB according to3.4. Get:5. S (n) = S (N-1) + D (N-1); On the assumption with 5. can get f (n-1) = S (n) pushed to get: s (N-1) = f (N-2); bring in Target. Get f (n)=2*f (N-1) + F (N-2);
Dynamic programming: Derivation formula of routines dark string bull