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
The problem is to ask for a B C three-character permutation, then exclude the pure string to solve the number of Diablo.
Obviously the input 1 output is 3 because the presence of A B C three combinations are dark
Input 2 exists AA AB AC BA BB BC CA CB CC 9 combinations, because the longest is 2 so all is Diablo
Input 3 full array minus ABC ACB BAC BCA CBA CAB 6 combinations
Input 4 is all arranged and then minus the number of substrings in the string that exist in the 6 combination is Diablo.
So this problem, as long as the internet search for a full array of algorithms, and then with the KMP algorithm can be
NetEase 2017 Diablo String