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 C++14 (GCC 5.4.1) (judge verdict: Accepted)


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

static void D_WeLikeAGC();	// https://atcoder.jp/contests/abc122/tasks/abc122_d

int main()
{
	D_WeLikeAGC();

	fflush(stdout);
	return 0;
}

enum CharKinds
{
	charA = 0,
	charC,
	charG,
	charT,

	charKind,
};

class RecordedPattern {
public:
	std::vector<std::string> vStrPtns;
	std::vector<unsigned long long> vPtnCnt;

	RecordedPattern() {}
	~RecordedPattern() {}
	

	void set(const char* szPtn, unsigned long long ptnCnt)
	{
		if (!match(szPtn, ptnCnt)) {
			vStrPtns.push_back(szPtn);
			vPtnCnt.push_back(ptnCnt);
		}
	}

	bool match(const char* szPtn, unsigned long long & ptnCnt)
	{
		bool isMatch = false;
		int strCnt = vStrPtns.size();

		for (int i = 0; i < strCnt; i++) {
			if (vStrPtns[i] == szPtn) {
				isMatch = true;
				ptnCnt = vPtnCnt[i];
				break;
			}
		}
		return isMatch;
	}
};

static RecordedPattern vRecords[100];

bool isAvailable(const std::string & targ)
{
	bool avail = true;
	int len = targ.length();
	const char* cmp = targ.c_str();

	if ((strstr(cmp, "AGC")) ||
		(strstr(cmp, "GAC")) ||
		(strstr(cmp, "ACG"))) {
		avail = false;
	}
	else if ((len > 3) && (cmp[0] == 'A') && (cmp[3] == 'C')) {
		if ((cmp[1] == 'G') || (cmp[2] == 'G')) {
			avail = false;
		}
	}
	return avail;
}

unsigned long long decideChar(int maxN, int szIdx, const char * szBfr)
{
	static const char available[] = { 'A', 'C', 'G', 'T' };
	static const int TmpSzLen = 4;

	int chkCnt = (TmpSzLen > szIdx) ? szIdx : TmpSzLen;
	char mkStr[TmpSzLen + 1] = { 0 };
	unsigned long long patternCnt = 0;

	if (szIdx >= maxN) {
		return 1;
	}


	if (vRecords[szIdx - 1].match(szBfr, patternCnt)) {
		return patternCnt;
	}

	int cpyOfs = 0;
	if (chkCnt >= TmpSzLen) {
		cpyOfs = 1;
	}
	
	for (int j = 0; j < (chkCnt - cpyOfs); j++) {
		mkStr[j] = szBfr[j + cpyOfs];
	}

	std::string chk = "";
	unsigned long long tmpRet = 0;
	for (int i = 0; i < charKind; i++) {
		tmpRet = 0;
		mkStr[chkCnt - cpyOfs] = available[i];
		
		chk = mkStr;
		if (isAvailable(chk)) {
			tmpRet = decideChar(maxN, szIdx + 1, mkStr);
		}
		patternCnt += tmpRet;
	}
	patternCnt = patternCnt % ((unsigned long long)(1e9) + 7);
	vRecords[szIdx - 1].set(szBfr, patternCnt);

	return patternCnt;
}

static void D_WeLikeAGC()
{
	const char addable[] = { 'A', 'C', 'G', 'T' };
	int N = 0;

	scanf(" %d", &N);

	char buff[4] = { 0 };
	unsigned long long pattern = 0;

	for (int i = 0; i < charKind; i++) {
		buff[0] = addable[i];
		pattern += decideChar(N, 1, buff);
	}
	
	printf("%llu\n",  (unsigned long long)(pattern % (unsigned long long)(1e9 + 7)));
}