結果

問題 No.1512 作文
ユーザー tentententen
提出日時 2021-05-25 11:06:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 918 ms / 2,000 ms
コード長 2,223 bytes
コンパイル時間 3,765 ms
コンパイル使用メモリ 78,776 KB
実行使用メモリ 130,144 KB
最終ジャッジ日時 2024-04-21 21:02:24
合計ジャッジ時間 11,401 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
37,196 KB
testcase_01 AC 49 ms
36,952 KB
testcase_02 AC 48 ms
36,832 KB
testcase_03 AC 48 ms
36,908 KB
testcase_04 AC 539 ms
88,888 KB
testcase_05 AC 529 ms
81,560 KB
testcase_06 AC 582 ms
88,992 KB
testcase_07 AC 515 ms
81,532 KB
testcase_08 AC 518 ms
81,828 KB
testcase_09 AC 98 ms
39,644 KB
testcase_10 AC 101 ms
39,864 KB
testcase_11 AC 100 ms
39,612 KB
testcase_12 AC 98 ms
39,720 KB
testcase_13 AC 101 ms
39,552 KB
testcase_14 AC 133 ms
42,596 KB
testcase_15 AC 128 ms
42,432 KB
testcase_16 AC 138 ms
42,384 KB
testcase_17 AC 138 ms
42,288 KB
testcase_18 AC 137 ms
42,268 KB
testcase_19 AC 133 ms
42,280 KB
testcase_20 AC 139 ms
42,336 KB
testcase_21 AC 50 ms
36,760 KB
testcase_22 AC 47 ms
37,004 KB
testcase_23 AC 52 ms
36,924 KB
testcase_24 AC 54 ms
37,220 KB
testcase_25 AC 54 ms
37,172 KB
testcase_26 AC 46 ms
36,768 KB
testcase_27 AC 44 ms
37,080 KB
testcase_28 AC 45 ms
36,644 KB
testcase_29 AC 47 ms
36,908 KB
testcase_30 AC 47 ms
36,844 KB
testcase_31 AC 47 ms
36,972 KB
testcase_32 AC 46 ms
37,244 KB
testcase_33 AC 46 ms
37,076 KB
testcase_34 AC 58 ms
37,532 KB
testcase_35 AC 99 ms
39,776 KB
testcase_36 AC 99 ms
39,920 KB
testcase_37 AC 90 ms
39,144 KB
testcase_38 AC 110 ms
39,992 KB
testcase_39 AC 107 ms
40,632 KB
testcase_40 AC 733 ms
130,144 KB
testcase_41 AC 918 ms
110,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static ArrayList<String> list = new ArrayList<>();
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            String s = sc.next();
            if (check(s)) {
                list.add(s);
            }
        }
        Collections.sort(list, new Comparator<String>() {
            public int compare(String s1, String s2) {
                if (s1.charAt(s1.length() - 1) == s2.charAt(s2.length() - 1)) {
                    return s1.charAt(0) - s2.charAt(0);
                } else {
                    return s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1);
                }
            }
        });
        dp = new int[list.size()][26];
        System.out.println(dfw(list.size() - 1, 25));
    }
    
    static boolean check(String s) {
        char start = 'a';
        for (char c : s.toCharArray()) {
            if (c < start) {
                return false;
            }
            start = c;
        }
        return true;
    }
    
    static int dfw(int idx, int alpha) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][alpha] == 0) {
            String s = list.get(idx);
            if (s.charAt(s.length() - 1) - 'a' > alpha) {
                dp[idx][alpha] = dfw(idx - 1, alpha);
            } else {
                dp[idx][alpha] = Math.max(dfw(idx - 1, alpha), dfw(idx - 1, s.charAt(0) - 'a') + s.length());
            }
        }
        return dp[idx][alpha];
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0