Problem:
Score : 400 points

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

The string does not contain characters other than A, C, G and T.

The string does not contain AGC as a substring.

The condition above cannot be violated by swapping two adjacent characters once.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

61

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

Sample Input 2

4

Sample Output 2

230

Sample Input 3

100

Sample Output 3

388130742

Be sure to print the number of strings modulo 10^9+7.

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::collections::HashMap;
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

const MOD: i64 = 1e+9 as i64 + 7;

fn calc(n: usize, depth: usize, prev: Vec<char>, memo: &mut Vec<HashMap<Vec<char>, i64>>) -> i64 {
    if depth == n {
        return 1;
    }
    if let Some(&ret) = memo[depth].get(&prev) {
        return ret;
    }
    let mut ret = 0;
    for &c in ['A', 'G', 'C', 'T'].iter() {
        if prev.len() >= 2 {
            let mut last_two = vec![];
            last_two.push(prev[prev.len() - 2]);
            last_two.push(prev[prev.len() - 1]);
            if last_two == vec!['A', 'G'] && c == 'C'
                || last_two == vec!['G', 'A'] && c == 'C'
                || last_two == vec!['A', 'C'] && c == 'G'
            {
                continue;
            }
        }
        if prev.len() == 3
            && ((prev[0] == 'A' && prev[1] == 'G' && c == 'C')
                || (prev[0] == 'A' && prev[2] == 'G' && c == 'C'))
        {
            continue;
        }
        let mut next = prev.clone();
        next.push(c);
        // println!("{:?}", next);
        if next.len() == 4 {
            next.remove(0);
        }
        ret = (ret + calc(n, depth + 1, next, memo)) % MOD;
    }
    memo[depth].insert(prev, ret);
    ret
}

fn main() {
    let n = read::<usize>();
    let mut memo = vec![HashMap::<Vec<char>, i64>::new(); n];
    let ans = calc(n, 0, vec![], &mut memo);
    println!("{}", ans);
}