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

ABC215-E Chain Contestant


問題へのリンク


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("4");
            WillReturn.Add("BGBH");
            //13
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("100");
            WillReturn.Add("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF");
            //330219020
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const int Hou = 998244353;

    static void Main()
    {
        List<string> InputList = GetInputList();
        string S = InputList[1];

        int AllBitOn = (1 << 10) - 1;

        // 場合の数[参加したコンテスト,最後に参加したコンテスト]なDP表
        int[,] PrevDP = new int[AllBitOn + 1, 10 + 1];
        PrevDP[0, 10] = 1;

        foreach (char EachChar in S) {
            int CurrContest = EachChar - 'A';
            int CurrBit = (1 << (CurrContest));

            int[,] CurrDP = (int[,])PrevDP.Clone();
            for (int I = 0; I <= AllBitOn; I++) {
                for (int J = 0; J <= 10; J++) {
                    if (PrevDP[I, J] == 0) continue;

                    if (J != 10 && J != CurrContest) {
                        if ((I & CurrBit) > 0) {
                            continue;
                        }
                    }

                    int NewI = I | CurrBit;
                    int NewJ = CurrContest;

                    CurrDP[NewI, NewJ] += PrevDP[I, J];
                    CurrDP[NewI, NewJ] %= Hou;
                }
            }
            PrevDP = CurrDP;
        }

        int Answer = 0;
        for (int I = 1; I <= AllBitOn; I++) {
            for (int J = 0; J <= 10; J++) {
                Answer += PrevDP[I, J];
                Answer %= Hou;
            }
        }
        Console.WriteLine(Answer);
    }
}


解説

ビットDPで解いてます。