// あなたのコーディングスキルを丸裸に!
// http://w...content-available-to-author-only...o.jp/enterprise/articles/1004/03/news002.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mahjong
{
class Program
{
static string[] machi =
{
"11", "22", "33", "44", "55", "66", "77", "88", "99", // shabo
"12", "23", "34", "45", "56", "67", "78", "89", // ryanmen penchan
"13", "24", "35", "46", "57", "68", "79", // kanchan
};
static string[] mentsu =
{
"123", "234", "345", "456", "567", "678", "789", // shuntsu
"111", "222", "333", "444", "555", "666", "777", "888", "999", // kohtsu
};
static string[] atama =
{
"11", "22", "33", "44", "55", "66", "77", "88", "99",
};
static bool TryExtract(string input, string mentsu, out string remains)
{
// input から mentsu を抜ければ抜いて残りをremainsに。
List<char> chr = new List<char>(input);
bool ok = true;
foreach (var c in mentsu)
ok &= chr.Remove(c);
remains = new string(chr.ToArray());
return ok;
}
static void Find(string remains, int index, string current, Action<string> onFound)
{
// 抜ける三枚組を抜いて再帰探索。
//Console.Error.WriteLine(current + " / " + remains);
string next;
if (remains.Length == 1)
{
onFound(current + "[" + remains + "]"); // tanki
}
else if (remains.Length == 2)
{
if (machi.Contains(remains)) // is machi?
onFound(current + "[" + remains + "]"); // ok
}
else
{
for (int i = index; i < mentsu.Length; i++)
if (TryExtract(remains, mentsu[i], out next))
Find(next, i, current + "(" + mentsu[i] + ")", onFound);
}
}
static void Find(string input, Action<string> onFound)
{
string next;
// 雀頭を抜いて探索
foreach (var m in atama)
if (TryExtract(input, m, out next))
Find(next, 0, "(" + m + ")", onFound);
// 単騎待ち仮定。
Find(input, 0, "", onFound);
// てぃんいとぅてぃーとぅいとぅ
// こーつがない && たーつ抜けるだけ抜いたら1枚になった?
}
static void Main(string[] args)
{
Array.Sort(mentsu);
while (true)
{
string input = Console.ReadLine();
if (input == null) break;
Console.WriteLine("Input: " + input);
Find(input, Console.WriteLine);
Console.WriteLine("end.");
}
}
}
}
Ly8g44GC44Gq44Gf44Gu44Kz44O844OH44Kj44Oz44Kw44K544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ciLCAiODgiLCAiOTkiLAogICAgICAgIH07CgogICAgICAgIHN0YXRpYyBib29sIFRyeUV4dHJhY3Qoc3RyaW5nIGlucHV0LCBzdHJpbmcgbWVudHN1LCBvdXQgc3RyaW5nIHJlbWFpbnMpCiAgICAgICAgewogICAgICAgICAgICAvLyBpbnB1dCDjgYvjgokgbWVudHN1IOOCkuaKnOOBkeOCjOOBsOaKnOOBhOOBpuaui+OCiuOCknJlbWFpbnPjgavjgIIKICAgICAgICAgICAgTGlzdDxjaGFyPiBjaHIgPSBuZXcgTGlzdDxjaGFyPihpbnB1dCk7CiAgICAgICAgICAgIGJvb2wgb2sgPSB0cnVlOwogICAgICAgICAgICBmb3JlYWNoICh2YXIgYyBpbiBtZW50c3UpCiAgICAgICAgICAgICAgICBvayAmPSBjaHIuUmVtb3ZlKGMpOwoKICAgICAgICAgICAgcmVtYWlucyA9IG5ldyBzdHJpbmcoY2hyLlRvQXJyYXkoKSk7CiAgICAgICAgICAgIHJldHVybiBvazsKICAgICAgICB9CgogICAgICAgIHN0YXRpYyB2b2lkIEZpbmQoc3RyaW5nIHJlbWFpbnMsIGludCBpbmRleCwgc3RyaW5nIGN1cnJlbnQsIEFjdGlvbjxzdHJpbmc+IG9uRm91bmQpCiAgICAgICAgewogICAgICAgICAgICAvLyDmipzjgZHjgovkuInmnprntYTjgpLmipzjgYTjgablho3luLDmjqLntKLjgIIKICAgICAgICAgICAgLy9Db25zb2xlLkVycm9yLldyaXRlTGluZShjdXJyZW50ICsgIiAvICIgKyByZW1haW5zKTsKICAgICAgICAgICAgc3RyaW5nIG5leHQ7CiAgICAgICAgICAgIGlmIChyZW1haW5zLkxlbmd0aCA9PSAxKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBvbkZvdW5kKGN1cnJlbnQgKyAiWyIgKyByZW1haW5zICsgIl0iKTsgLy8gdGFua2kKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChyZW1haW5zLkxlbmd0aCA9PSAyKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAobWFjaGkuQ29udGFpbnMocmVtYWlucykpIC8vIGlzIG1hY2hpPwogICAgICAgICAgICAgICAgICAgIG9uRm91bmQoY3VycmVudCArICJbIiArIHJlbWFpbnMgKyAiXSIpOyAvLyBvawogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IGluZGV4OyBpIDwgbWVudHN1Lkxlbmd0aDsgaSsrKQogICAgICAgICAgICAgICAgICAgIGlmIChUcnlFeHRyYWN0KHJlbWFpbnMsIG1lbnRzdVtpXSwgb3V0IG5leHQpKQogICAgICAgICAgICAgICAgICAgICAgICBGaW5kKG5leHQsIGksIGN1cnJlbnQgKyAiKCIgKyBtZW50c3VbaV0gKyAiKSIsIG9uRm91bmQpOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBzdGF0aWMgdm9pZCBGaW5kKHN0cmluZyBpbnB1dCwgQWN0aW9uPHN0cmluZz4gb25Gb3VuZCkKICAgICAgICB7CiAgICAgICAgICAgIHN0cmluZyBuZXh0OwogICAgICAgICAgICAvLyDpm4DpoK3jgpLmipzjgYTjgabmjqLntKIKICAgICAgICAgICAgZm9yZWFjaCAodmFyIG0gaW4gYXRhbWEpCiAgICAgICAgICAgICAgICBpZiAoVHJ5RXh0cmFjdChpbnB1dCwgbSwgb3V0IG5leHQpKQogICAgICAgICAgICAgICAgICAgIEZpbmQobmV4dCwgMCwgIigiICsgbSArICIpIiwgb25Gb3VuZCk7CgogICAgICAgICAgICAvLyDljZjpqI7lvoXjgaHku67lrprjgIIKICAgICAgICAgICAgRmluZChpbnB1dCwgMCwgIiIsIG9uRm91bmQpOwoKICAgICAgICAgICAgLy8g44Gm44GD44KT44GE44Go44GF44Gm44GD44O844Go44GF44GE44Go44GFCiAgICAgICAgICAgIC8vIOOBk+ODvOOBpOOBjOOBquOBhCAmJiDjgZ/jg7zjgaTmipzjgZHjgovjgaDjgZHmipzjgYTjgZ/jgokx5p6a44Gr44Gq44Gj44Gf77yfCiAgICAgICAgfQoKICAgICAgICBzdGF0aWMgdm9pZCBNYWluKHN0cmluZ1tdIGFyZ3MpCiAgICAgICAgewogICAgICAgICAgICBBcnJheS5Tb3J0KG1lbnRzdSk7CgogICAgICAgICAgICB3aGlsZSAodHJ1ZSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc3RyaW5nIGlucHV0ID0gQ29uc29sZS5SZWFkTGluZSgpOwogICAgICAgICAgICAgICAgaWYgKGlucHV0ID09IG51bGwpIGJyZWFrOwogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIklucHV0OiAiICsgaW5wdXQpOwogICAgICAgICAgICAgICAgRmluZChpbnB1dCwgQ29uc29sZS5Xcml0ZUxpbmUpOwogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoImVuZC4iKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQ==