Legal string "Dynamic planning"

Source: Internet
Author: User

Strings can consist of a, B, and C three letters, and if any of the next three letters are the same, they are illegal. How many valid strings of length n are available? For example: ABBBCA is illegal, accbcca is legal.

The idea of dynamic planning-do you really want to enumerate?
DP[I][0]: The number of the last two different valid strings of length I
DP[I][1]: The number of the last two bits of the same valid string in length I
Recursion: dp[i][0] = (dp[i-1][0] * 2 + dp[i-1][1] * 2)
DP[I][1] = dp[i-1][0]

Initial
Dp[1][0] = 3, dp[1][1] = 0
Results
Dp[n][0] + dp[n][1]
Space optimization
dp[i][0,1] Only related to dp[i-1][0,1], you can dispense with high-dimensional
Complexity of Time
O (N)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Legal string "Dynamic planning"

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.