結果

問題 No.1512 作文
ユーザー bluemeganebluemegane
提出日時 2021-11-04 18:25:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 1,911 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 68,552 KB
実行使用メモリ 63,188 KB
最終ジャッジ日時 2023-08-04 20:17:51
合計ジャッジ時間 7,239 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
23,712 KB
testcase_01 AC 62 ms
21,776 KB
testcase_02 AC 60 ms
21,520 KB
testcase_03 AC 61 ms
23,680 KB
testcase_04 AC 235 ms
45,892 KB
testcase_05 AC 241 ms
45,716 KB
testcase_06 AC 240 ms
43,748 KB
testcase_07 AC 231 ms
43,796 KB
testcase_08 AC 234 ms
45,812 KB
testcase_09 AC 84 ms
22,496 KB
testcase_10 AC 83 ms
22,480 KB
testcase_11 AC 87 ms
24,352 KB
testcase_12 AC 89 ms
22,692 KB
testcase_13 AC 86 ms
24,648 KB
testcase_14 AC 84 ms
27,936 KB
testcase_15 AC 83 ms
25,856 KB
testcase_16 AC 86 ms
23,744 KB
testcase_17 AC 85 ms
23,568 KB
testcase_18 AC 83 ms
25,796 KB
testcase_19 AC 84 ms
27,816 KB
testcase_20 AC 82 ms
25,840 KB
testcase_21 AC 58 ms
21,424 KB
testcase_22 AC 62 ms
23,616 KB
testcase_23 AC 64 ms
23,608 KB
testcase_24 AC 64 ms
21,680 KB
testcase_25 AC 64 ms
21,684 KB
testcase_26 AC 60 ms
21,552 KB
testcase_27 AC 58 ms
19,356 KB
testcase_28 AC 58 ms
23,580 KB
testcase_29 AC 62 ms
21,668 KB
testcase_30 AC 60 ms
23,564 KB
testcase_31 AC 63 ms
21,488 KB
testcase_32 AC 64 ms
21,636 KB
testcase_33 AC 64 ms
21,580 KB
testcase_34 AC 64 ms
19,552 KB
testcase_35 AC 88 ms
22,788 KB
testcase_36 AC 87 ms
20,612 KB
testcase_37 AC 76 ms
22,156 KB
testcase_38 AC 81 ms
21,716 KB
testcase_39 AC 77 ms
21,636 KB
testcase_40 AC 395 ms
60,972 KB
testcase_41 AC 274 ms
63,188 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)
                {
                    dp[i + 1, j] = Max(dp[i + 1, j], ww);
                    if (ps[i + 1].start >= j)
                    {
                        var nextend = ps[i + 1].end;
                        dp[i + 1, nextend] = Max(dp[i + 1, nextend], ww + ps[i + 1].L);
                    }
                }
            }
        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