結果

問題 No.437 cwwゲーム
ユーザー bluemegane
提出日時 2018-10-10 09:57:29
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 2,365 bytes
コンパイル時間 2,579 ms
コンパイル使用メモリ 116,148 KB
実行使用メモリ 24,064 KB
最終ジャッジ日時 2024-10-12 08:48:01
合計ジャッジ時間 5,399 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System.Linq;
using System.Collections.Generic;
using System;

public static class combi
{
    public static IEnumerable<IEnumerable<T>> Comb<T>(this IEnumerable<T> items, int r)
    {
        if (r == 0)
        {
            yield return Enumerable.Empty<T>();
        }
        else
        {
            var i = 1;
            foreach (var x in items)
            {
                var xs = items.Skip(i);
                foreach (var c in Comb(xs, r - 1))
                    yield return c.Before(x);
                i++;
            }
        }
    }
    public static IEnumerable<T> Before<T>(this IEnumerable<T> items, T first)
    {
        yield return first;
        foreach (var i in items)
            yield return i;
    }
}

public class P
{
    public int score { get; set; }
    public int[] a { get; set; }
}

public class Hello
{
    public static void Main()
    {
        var s = Console.ReadLine().Trim();
        var sL = s.Length;
        if (sL <= 2) { Console.WriteLine(0); goto end; }
        var a = new int[sL];
        var w = new P { a = new int[sL] };
        for (int i = 0; i < sL; i++) w.a[i] = int.Parse(s[i].ToString());
        var ans = new HashSet<int>();
        rec(w, ans);
        if (ans.Count() == 0) Console.WriteLine(0);
        else Console.WriteLine(ans.Max());
        end:;
    }
    public static void rec(P p, HashSet<int> ans)
    {
        var aL = p.a.Length;
        if (aL <= 2 && p.score != 0) { ans.Add(p.score); return; }
        ans.Add(p.score);
        foreach (var x in Enumerable.Range(0, aL).Comb(3))
        {
            var w = x.ToArray();
            var c = checkCWW(p.a, w);
            if (c > 0)
            {
                ans.Add(c);
                if (aL - 3 >= 1)
                {
                    var w2 = new P { a = new int[aL - 3], score = p.score + c };
                    var pt = 0;
                    for (int i = 0; i < aL; i++)
                        if (!w.Contains(i)) w2.a[pt++] = p.a[i];
                    rec(w2, ans);
                }
                else ans.Add(p.score + c);
            }
        }
    }
    public static int checkCWW(int[] a, int[] b)
    {
        if (a[b[0]] == 0) return -1;
        if (a[b[0]] == a[b[1]]) return -1;
        if (a[b[1]] == a[b[2]]) return 100 * a[b[0]] + 10 * a[b[1]] + a[b[2]];
        return -1;
    }
}
0