TopCoder problem "ForbiddenStrings" used in SRM 412 (Division I Level One)



Problem Statement

    A string of letters A, B, C is forbidden if there are three consecutive letters from which one is A, one is B, and one is C. For example, BAACAACCBAAA is forbidden, while AABBCCAABB is not.



Your task is to calculate how many such strings of length n are not forbidden.
 

Definition

    
Class:ForbiddenStrings
Method:countNotForbidden
Parameters:int
Returns:long
Method signature:long countNotForbidden(int n)
(be sure your method is public)
    
 

Constraints

-n will be between 1 and 30, inclusive.
 

Examples

0)
    
2
Returns: 9
All 9 strings of length 2 are not forbidden.
1)
    
3
Returns: 21
There are 27 strings of length 3. Of these, 6 contain one occurrence of each letter. Those 6 strings are forbidden, so you should return 21.
2)
    
4
Returns: 51

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8480

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13503&pm=8480

Writer:

Eryx

Testers:

PabloGilberto , Olexiy , Andrew_Lazarev , ivan_metelsky

Problem categories:

Dynamic Programming, Math