結果

問題 No.437 cwwゲーム
ユーザー 14番14番
提出日時 2016-10-28 23:42:35
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,598 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 115,276 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2024-10-12 08:26:22
合計ジャッジ時間 3,520 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        long ans = GetAns(long.Parse(Reader.ReadLine()), new int[]{});
        Console.WriteLine(ans);
    }

    private Dictionary<long, long> dic = new Dictionary<long, long>();
    

    private long GetAns(long num, int[] idx) {
        if(idx.Length == 0 && dic.ContainsKey(num)) {
            return dic[num];
        }

        long ans = 0;

        string numstr = num.ToString();
        if(numstr.Length + idx.Length < 3) {
            return 0;
        }

        int start = 0;
        if(idx.Length > 0) {
            start = idx.Last() + 1;
        }
        for(int i=start; i<numstr.Length; i++) {
            if(idx.Length == 0) {
                if(numstr[i] == '0') {
                    continue;
                }
                long ret = this.GetAns(num, new int[]{i});
                ans = Math.Max(ans, ret);
            } else if(idx.Length == 1) {
                if(numstr[i] == numstr[idx[0]]) {
                    continue;
                }
                long ret = this.GetAns(num, new int[]{idx[0], i});
                ans = Math.Max(ans, ret);
            } else {
                if(numstr[i] != numstr[idx.Last()]) {
                    continue;
                }
                int addNum = int.Parse(numstr[idx[0]].ToString() + numstr[idx[1]] + numstr[i]);
                List<char> tmp = numstr.ToList();
                tmp.RemoveAt(i);
                tmp.RemoveAt(idx[1]);
                tmp.RemoveAt(idx[0]);
                long ret = 0;
                if(tmp.Count > 0) {
                    ret = this.GetAns(int.Parse(string.Join(string.Empty, tmp)), new int[] {});
                }
                ans = Math.Max(ans, ret + addNum);
            }
        }
        if(idx.Length == 0) {
            dic.Add(num, ans);
        }
        return ans;
    }

    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"


133144



";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0