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

ABC279-C RANDOM


問題へのリンク


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("3 4");
            WillReturn.Add("##.#");
            WillReturn.Add("##..");
            WillReturn.Add("#...");
            WillReturn.Add(".###");
            WillReturn.Add("..##");
            WillReturn.Add("...#");
            //Yes
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3");
            WillReturn.Add("#.#");
            WillReturn.Add(".#.");
            WillReturn.Add("#.#");
            WillReturn.Add("##.");
            WillReturn.Add("##.");
            WillReturn.Add(".#.");
            //No
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 1");
            WillReturn.Add("#");
            WillReturn.Add(".");
            WillReturn.Add("#");
            WillReturn.Add(".");
            //Yes
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("8 7");
            WillReturn.Add("#..#..#");
            WillReturn.Add(".##.##.");
            WillReturn.Add("#..#..#");
            WillReturn.Add(".##.##.");
            WillReturn.Add("#..#..#");
            WillReturn.Add(".##.##.");
            WillReturn.Add("#..#..#");
            WillReturn.Add(".##.##.");
            WillReturn.Add("....###");
            WillReturn.Add("####...");
            WillReturn.Add("....###");
            WillReturn.Add("####...");
            WillReturn.Add("....###");
            WillReturn.Add("####...");
            WillReturn.Add("....###");
            WillReturn.Add("####...");
            //Yes
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int UB_X;
    static int UB_Y;

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int H = wkArr[0];

        char[,] Ban1 = CreateBanArr(InputList.Skip(1).Take(H));
        UB_X = Ban1.GetUpperBound(0);
        UB_Y = Ban1.GetUpperBound(1);

        char[,] Ban2 = CreateBanArr(InputList.Skip(1 + H));

        var HashCntDict1 = new Dictionary<string, int>();
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            string Hash = GetHash(LoopX, Ban1);
            if (HashCntDict1.ContainsKey(Hash) == false) {
                HashCntDict1[Hash] = 0;
            }
            HashCntDict1[Hash]++;
        }

        var HashCntDict2 = new Dictionary<string, int>();
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            string Hash = GetHash(LoopX, Ban2);
            if (HashCntDict2.ContainsKey(Hash) == false) {
                HashCntDict2[Hash] = 0;
            }
            HashCntDict2[Hash]++;
        }

        if (HashCntDict1.OrderBy(pX => pX.Key).SequenceEqual(HashCntDict2.OrderBy(pX => pX.Key))) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }

    // X座標を引数として、ハッシュ値を返す
    static string GetHash(int pX, char[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            sb.Append(pBanArr[pX, LoopY]);
        }
        return sb.ToString();
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をcharの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = StrList[0].Length - 1;
        int UB_Y = StrList.Count - 1;

        char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = StrList[Y][X];
            }
        }
        return WillReturn;
    }
}


解説

列ごとのハッシュ値の度数分布表を作って、
同じDictionary型同士でSequenceEqualメソッドで比較してます