# 552.student-attendance-record-ii

## Statement

• `'A'`：Absent，缺勤
• `'L'`：Late，迟到
• `'P'`：Present，到场

• 总出勤 计，学生缺勤（`'A'`严格 少于两天。
• 学生 不会 存在 连续 3 天或 连续 3 天以上的迟到（`'L'`）记录。

``````输入：n = 2

"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"

``````

``````输入：n = 1

``````

``````输入：n = 10101

``````

• `1 <= n <= 105`

An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

• `'A'`: Absent.
• `'L'`: Late.
• `'P'`: Present.

Any student is eligible for an attendance award if they meet both of the following criteria:

• The student was absent (`'A'`) for strictly fewer than 2 days total.
• The student was never late (`'L'`) for 3 or more consecutive days.

Given an integer `n`, return the number of possible attendance records of length `n` that make a student eligible for an attendance award. The answer may be very large, so return it modulo `109 + 7`.

Example 1:

``````Input: n = 2
Output: 8
Explanation: There are 8 records with length 2 that are eligible for an award:
"PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).
``````

Example 2:

``````Input: n = 1
Output: 3
``````

Example 3:

``````Input: n = 10101
Output: 183236316
``````

Constraints:

• `1 <= n <= 105`

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

template <typename T>
inline void chmod(T &a, const T &b) {
a += b;
if (a > mod) {
a -= mod;
}
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

const int N = 1e5 + 10;
int f[N][3][4];

class Solution {
public:
int checkRecord(int n) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
f[i][j][k] = 0;
}
}
}

f[0][0][0] = 1;

for (int i = 1; i <= n; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
chmod(f[i][j][0], f[i - 1][j][k]);

if (j) {
chmod(f[i][j][0], f[i - 1][j - 1][k]);
}

if (k) {
chmod(f[i][j][k], f[i - 1][j][k - 1]);
}
}
}
}

int res = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
chmod(res, f[n][i][j]);
}
}

return res;
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````