AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC278-F Shiritori


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("6");
            WillReturn.Add("enum");
            WillReturn.Add("float");
            WillReturn.Add("if");
            WillReturn.Add("modint");
            WillReturn.Add("takahashi");
            WillReturn.Add("template");
            //First
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10");
            WillReturn.Add("catch");
            WillReturn.Add("chokudai");
            WillReturn.Add("class");
            WillReturn.Add("continue");
            WillReturn.Add("copy");
            WillReturn.Add("exec");
            WillReturn.Add("havoc");
            WillReturn.Add("intrinsic");
            WillReturn.Add("static");
            WillReturn.Add("yucatec");
            //Second
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("16");
            WillReturn.Add("mnofcmzsdx");
            WillReturn.Add("lgeowlxuqm");
            WillReturn.Add("ouimgdjxlo");
            WillReturn.Add("jhwttcycwl");
            WillReturn.Add("jbcuioqbsj");
            WillReturn.Add("mdjfikdwix");
            WillReturn.Add("jhvdpuxfil");
            WillReturn.Add("peekycgxco");
            WillReturn.Add("sbvxszools");
            WillReturn.Add("xuuqebcrzp");
            WillReturn.Add("jsciwvdqzl");
            WillReturn.Add("obblxzjhco");
            WillReturn.Add("ptobhnpfpo");
            WillReturn.Add("muizaqtpgx");
            WillReturn.Add("jtgjnbtzcl");
            WillReturn.Add("sivwidaszs");
            //First
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    static int mN;

    static List<string> mSList = new List<string>();

    static void Main()
    {
        List<string> InputList = GetInputList();
        mN = int.Parse(InputList[0]);

        foreach (string EachStr in InputList.Skip(1)) {
            mSList.Add(EachStr);
        }

        if (IsTebanWin(0, "?")) {
            Console.WriteLine("First");
        }
        else {
            Console.WriteLine("Second");
        }
    }

    // 手番を持った人の勝ちか?
    static Dictionary<string, bool> mMemo = new Dictionary<string, bool>();

    static bool IsTebanWin(int pUsedBit, string pNeedFirstStr)
    {
        string Hash = string.Format("{0},{1}", pUsedBit, pNeedFirstStr);
        if (mMemo.ContainsKey(Hash)) {
            return mMemo[Hash];
        }

        var SeniList = new List<bool>();
        for (int I = 0; I <= mSList.Count - 1; I++) {
            int CurrBit = (1 << I);
            string CurrStr = mSList[I];

            if ((pUsedBit & CurrBit) > 0) continue;

            if (pNeedFirstStr != "?") {
                if (CurrStr.StartsWith(pNeedFirstStr) == false) {
                    continue;
                }
            }
            int NewBit = pUsedBit + CurrBit;
            string NewStr = CurrStr.Substring(CurrStr.Length - 1);
            SeniList.Add(IsTebanWin(NewBit, NewStr));
        }

        bool Result = SeniList.Exists(pX => pX == false);
        return mMemo[Hash] = Result;
    }
}


解説

手番を持った人の勝ちか?
をメモ化再帰で求めてます。

状態数は
使用した文字列が 2**16 で 65536通り
次の先頭文字は16通り
65536*16 = 1048576
なので間に合います。