結果

問題 No.1512 作文
ユーザー bluemeganebluemegane
提出日時 2021-11-04 17:21:48
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 365 ms / 2,000 ms
コード長 2,023 bytes
コンパイル時間 1,737 ms
コンパイル使用メモリ 68,276 KB
実行使用メモリ 65,220 KB
最終ジャッジ日時 2023-08-04 19:04:33
合計ジャッジ時間 7,832 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
23,516 KB
testcase_01 AC 60 ms
21,564 KB
testcase_02 AC 60 ms
23,504 KB
testcase_03 AC 59 ms
21,524 KB
testcase_04 AC 223 ms
43,772 KB
testcase_05 AC 217 ms
43,752 KB
testcase_06 AC 226 ms
43,708 KB
testcase_07 AC 221 ms
43,764 KB
testcase_08 AC 217 ms
45,768 KB
testcase_09 AC 79 ms
22,476 KB
testcase_10 AC 80 ms
24,480 KB
testcase_11 AC 82 ms
22,420 KB
testcase_12 AC 80 ms
22,620 KB
testcase_13 AC 79 ms
22,508 KB
testcase_14 AC 79 ms
25,768 KB
testcase_15 AC 79 ms
27,700 KB
testcase_16 AC 80 ms
27,776 KB
testcase_17 AC 78 ms
25,712 KB
testcase_18 AC 79 ms
27,724 KB
testcase_19 AC 77 ms
25,872 KB
testcase_20 AC 79 ms
23,732 KB
testcase_21 AC 59 ms
23,384 KB
testcase_22 AC 60 ms
21,560 KB
testcase_23 AC 63 ms
21,460 KB
testcase_24 AC 61 ms
21,588 KB
testcase_25 AC 63 ms
21,556 KB
testcase_26 AC 58 ms
21,672 KB
testcase_27 AC 54 ms
23,360 KB
testcase_28 AC 60 ms
21,620 KB
testcase_29 AC 60 ms
23,704 KB
testcase_30 AC 60 ms
21,468 KB
testcase_31 AC 60 ms
21,496 KB
testcase_32 AC 60 ms
21,592 KB
testcase_33 AC 62 ms
23,632 KB
testcase_34 AC 60 ms
21,516 KB
testcase_35 AC 88 ms
24,600 KB
testcase_36 AC 86 ms
24,736 KB
testcase_37 AC 73 ms
21,932 KB
testcase_38 AC 77 ms
21,812 KB
testcase_39 AC 78 ms
19,624 KB
testcase_40 AC 365 ms
65,220 KB
testcase_41 AC 263 ms
61,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using static System.Math;
using System.Collections.Generic;
using System.Linq;
using System;

public class P
{
    public int start { get; set; }
    public int end { get; set; }
    public int L { get; set; }
}

public class Hello
{
    static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        var ps = new List<P>();
        for (int i = 0; i < n; i++)
        {
            var s = Console.ReadLine().Trim();
            var w = getP(s);
            if (w.start != -1) ps.Add(w);
        }
        getAns(ps);
    }
    static void getAns(List<P> ps)
    {
        var n = ps.Count();
        if (n == 0) { Console.WriteLine(0); return; }
        ps = ps.OrderBy(x => x.end).ThenBy(x => x.start).ToList();
        var dp = new int[n, 27];
        for (int i = 0; i < n; i++)
            for (int j = 1; j <= 26; j++) dp[i, j] = -1;
        var w = ps[0];
        dp[0, 0] = 0;
        dp[0, w.end] = w.L;
        for (int i = 0; i < n - 1; i++)
            for (int j = 0; j <= 26; j++)
            {
                var ww = dp[i, j];
                if (ww != -1)
                {
                    if (ps[i + 1].start < j) goto notselect;
                    var nextend = ps[i + 1].end;
                    var dpp = dp[i + 1, nextend];
                    dp[i + 1, nextend] = dpp == -1 ? ww + ps[i + 1].L : Max(dpp, ww + ps[i + 1].L);
                notselect:;
                    var dpp2 = dp[i + 1, j];
                    dp[i + 1, j] = dpp2 == -1 ? ww : Max(dpp2, ww);
                }
            }
        var ans = 0;
        for (int i = 1; i <= 26; i++)
        {
            if (dp[n - 1, i] != -1) ans = Max(ans, dp[n - 1, i]);
        }
        Console.WriteLine(ans);
    }
    static P getP(string s)
    {
        var sL = s.Length;
        var ss = s.ToCharArray();
        Array.Sort(ss);
        if (new string(ss) == s)
            return new P { start = s[0] - 'a' + 1, end = s[sL - 1] - 'a' + 1, L = sL };
        else return new P { start = -1 };
    }
}
0