AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC122-D We Like AGC
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
//61
}
else if (InputPattern == "Input2") {
WillReturn.Add("4");
//230
}
else if (InputPattern == "Input3") {
WillReturn.Add("100");
//388130742
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
const long Hou = 1000000007;
static void Main()
{
List<string> InputList = GetInputList();
long N = long.Parse(InputList[0]);
// 場合の数[最後の4文字]なDP表
var PrevDP = new Dictionary<string, long>();
PrevDP[""] = 1;
for (long I = 1; I <= N; I++) {
var CurrDP = new Dictionary<string, long>();
foreach (var EachPair in PrevDP) {
foreach (char EachChar in "ACGT") {
string NewKey = EachPair.Key + EachChar;
if (NewKey.EndsWith("AGC")) continue;
if (NewKey.EndsWith("GAC")) continue;
if (NewKey.EndsWith("ACG")) continue;
if (Regex.IsMatch(NewKey, "A.GC")) continue;
if (Regex.IsMatch(NewKey, "AG.C")) continue;
// 最後の4文字のみが必要な情報
if (NewKey.Length > 4) {
int StrUB = NewKey.Length - 1;
NewKey = NewKey.Substring(StrUB - 3);
}
if (CurrDP.ContainsKey(NewKey) == false) {
CurrDP[NewKey] = 0;
}
CurrDP[NewKey] += EachPair.Value;
CurrDP[NewKey] %= Hou;
}
}
PrevDP = CurrDP;
}
long Answer = 0;
foreach (long EachLong in PrevDP.Values) {
Answer += EachLong;
Answer %= Hou;
}
Console.WriteLine(Answer);
}
}
解説
最後の4文字を状態に持つ動的計画法で解いてます。