問題は「山折り谷折り」
この手の問題は関数型言語の方が向いているけれど、たまには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 "";
}
}
}