結果

問題 No.1512 作文
ユーザー tentententen
提出日時 2023-02-20 17:42:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 793 ms / 2,000 ms
コード長 2,462 bytes
コンパイル時間 5,166 ms
コンパイル使用メモリ 90,976 KB
実行使用メモリ 145,296 KB
最終ジャッジ日時 2023-09-28 14:46:59
合計ジャッジ時間 14,830 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
50,124 KB
testcase_01 AC 44 ms
49,788 KB
testcase_02 AC 45 ms
49,576 KB
testcase_03 AC 45 ms
49,608 KB
testcase_04 AC 617 ms
100,844 KB
testcase_05 AC 639 ms
101,320 KB
testcase_06 AC 634 ms
100,796 KB
testcase_07 AC 618 ms
101,368 KB
testcase_08 AC 639 ms
102,180 KB
testcase_09 AC 99 ms
52,204 KB
testcase_10 AC 87 ms
52,368 KB
testcase_11 AC 88 ms
49,780 KB
testcase_12 AC 89 ms
51,976 KB
testcase_13 AC 88 ms
52,268 KB
testcase_14 AC 146 ms
55,596 KB
testcase_15 AC 144 ms
55,820 KB
testcase_16 AC 144 ms
55,428 KB
testcase_17 AC 145 ms
55,844 KB
testcase_18 AC 144 ms
55,720 KB
testcase_19 AC 146 ms
55,928 KB
testcase_20 AC 146 ms
55,580 KB
testcase_21 AC 52 ms
50,120 KB
testcase_22 AC 48 ms
49,748 KB
testcase_23 AC 54 ms
47,636 KB
testcase_24 AC 53 ms
50,648 KB
testcase_25 AC 53 ms
50,344 KB
testcase_26 AC 45 ms
49,816 KB
testcase_27 AC 46 ms
49,612 KB
testcase_28 AC 45 ms
49,588 KB
testcase_29 AC 45 ms
49,668 KB
testcase_30 AC 46 ms
49,532 KB
testcase_31 AC 48 ms
49,576 KB
testcase_32 AC 49 ms
49,556 KB
testcase_33 AC 47 ms
49,584 KB
testcase_34 AC 70 ms
50,888 KB
testcase_35 AC 123 ms
53,004 KB
testcase_36 AC 118 ms
52,712 KB
testcase_37 AC 94 ms
53,024 KB
testcase_38 AC 129 ms
53,412 KB
testcase_39 AC 131 ms
53,832 KB
testcase_40 AC 793 ms
145,296 KB
testcase_41 AC 395 ms
125,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<String> base = 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++) {
            boolean enable = true;
            String s = sc.next();
            for (int j = 0; j < s.length() - 1 && enable; j++) {
                enable = (s.charAt(j) <= s.charAt(j + 1));
            }
            if (enable) {
                base.add(s);
            }
        }
        dp = new int[base.size()][26];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        Collections.sort(base);
        System.out.println(dfw(base.size() - 1, 25));
    }
    
    static int dfw(int idx, int v) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = dfw(idx - 1, v);
            String s = base.get(idx);
            if (s.charAt(s.length() - 1) - 'a' <= v) {
                dp[idx][v] = Math.max(dp[idx][v], dfw(idx - 1, s.charAt(0) - 'a') + s.length());
            }
        }
        return dp[idx][v];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0