NetEase 2017 Diablo String

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.