# 题目

Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.

A student attendance record is a string that only contains the following three characters:

1. ‘A’ : Absent.
2. ‘L’ : Late.
3. ‘P’ : Present.

A record is regarded as rewardable if it doesn’t contain more than one 'A' (absent) or more than two continuous 'L' (late).

Example 1:

Input: n = 2
Output: 8
Explanation:
There are 8 records with length 2 will be regarded as rewardable:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" won't be regarded as rewardable owing to more than one absent times. 

Note: The value of n won’t exceed 100,000.

# 思路一

### 原文思路

dp[i]the number of all possible attendance (without 'A') records with length i :

end with "P": dp[i-1]
end with "PL": dp[i-2]
end with "PLL": dp[i-3]
end with "LLL": is not allowed
so dp[i] = dp[i-1] + dp[i-2] + dp[i-3]

the number of all possible attendance (with 'A') records with length n:
∑dp[i] *dp[n-1-i] i = 0,1,...,n-1

Time Complexity O(n)
Space Complexity O(n)

1. 以P结尾
2. 以PL结尾
3. 以PLL结尾

dp[i] = dp[i-1] + dp[i-2] + dp[i-3]

∑dp[i] *dp[n-1-i] i = 0,1,...,n-1
What the fxxk is this!

For n=3, it becomes dp0dp2+dp1dp1+dp2dp0, When you place A at pos1, then P&L can be placed in dp0dp2 ways.看似没有直接解释，但倒是点通了我。

## 代码一

class Solution(object):
def checkRecord(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 0
if n == 1:
return 3
if n == 2:
return 8
MAX = 1000000007
dp = [1, 2, 4]#这里是手动计算出的n = 0，1，2时的dp值
i = 3
while i < n:
dp.append((dp[i - 1] + dp[i - 2] + dp[i - 3]) % MAX)
i += 1
result = (dp[n - 1] + dp[i - 2] + dp[i - 3]) % MAX
for i in range(n):
result += dp[i] * dp[n - i - 1] % MAX
result %= MAX
return result

# 思路二

At time t where every report is length t, Let a, b, c be sequence types without an 'A' ending in N, NL, LL; and d,e,f be sequence types with an 'A' ending in N, NL, LL. (Here, N will denote a non-'L' character.) These types are disjoint, and exhaustive (their union is the set of all valid reports.) At the beginning when t = 1, a = b = d = 1 and we should compute N-1 more steps.

From a sequence of type a, b, c, we can write an 'A' to give us a sequence of type d, or a 'P' to give us a sequence of type a. From a sequence of type d, e, f, we can write a 'P' to give us a sequence of type d. From a sequence of type a, b, d, e, we can write an 'L' to give a sequence of (respectively) b, c, e, f. These are all the letters we could write in any situation. Working backwards, we can get the sums for a,b,c,d,e,f written below.

def checkRecord(self, N):
MOD = 10**9 + 7
a = b = d = 1
c = e = f = 0
for _ in xrange(N-1):
a, b, c, d, e, f = (a+b+c)%MOD, a, b, (a+b+c+d+e+f)%MOD, d, e

return (a+b+c+d+e+f)%MOD