0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第18回オフラインリアルタイムどう書くの参考問題をC#で

Last updated at Posted at 2014-01-29

問題は「山折り谷折り
この手の問題は関数型言語の方が向いているけれど、たまにはC#でも。

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Folding = System.Collections.Generic.Dictionary<char, string>;

class Program
{
    static void Main(string[] args)
    {
        // 省略
    }

    static void Test(string input, string expected)
    {
        string actual = Solve(input);
        Console.WriteLine(expected == actual ? "OK" : "NG");
    }

    static Folding front = new Folding { { 'L', "bVf" }, { 'J', "fVb" }, { 'Z', "fmbVf" }, { 'U', "bVfVb" }, { 'S', "fVbmf" } };
    static Folding back = new Folding { { 'L', "bmf" }, { 'J', "fmb" }, { 'Z', "bmfVb" }, { 'U', "fmbmf" }, { 'S', "bVfmb" } };

    static string Solve(string input)
    {
        var folds = input
            .Aggregate("f".AsEnumerable(),
                       (xs, y) => xs.SelectMany(x => Fold(x, y)))
            .Where(x => x == 'm' || x == 'V');
        return new string(folds.ToArray());
    }

    static IEnumerable<char> Fold(char current, char input)
    {
        switch (current)
        {
            case 'f':
                return front[input];
            case 'b':
                return back[input];
            case 'm':
            case 'V':
                return new[] { current };
            default:
                return "";
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?