結果

問題 No.1512 作文
ユーザー tentententen
提出日時 2024-02-08 13:28:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 951 ms / 2,000 ms
コード長 2,649 bytes
コンパイル時間 5,325 ms
コンパイル使用メモリ 92,060 KB
実行使用メモリ 119,736 KB
最終ジャッジ日時 2024-02-08 13:28:42
合計ジャッジ時間 17,961 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
54,860 KB
testcase_01 AC 71 ms
54,888 KB
testcase_02 AC 71 ms
54,880 KB
testcase_03 AC 73 ms
54,864 KB
testcase_04 AC 951 ms
96,380 KB
testcase_05 AC 731 ms
98,436 KB
testcase_06 AC 717 ms
100,280 KB
testcase_07 AC 727 ms
98,068 KB
testcase_08 AC 735 ms
100,268 KB
testcase_09 AC 127 ms
56,044 KB
testcase_10 AC 127 ms
56,160 KB
testcase_11 AC 117 ms
56,032 KB
testcase_12 AC 127 ms
55,924 KB
testcase_13 AC 137 ms
56,160 KB
testcase_14 AC 298 ms
68,128 KB
testcase_15 AC 292 ms
68,072 KB
testcase_16 AC 294 ms
68,072 KB
testcase_17 AC 290 ms
68,252 KB
testcase_18 AC 296 ms
68,096 KB
testcase_19 AC 315 ms
68,088 KB
testcase_20 AC 294 ms
68,052 KB
testcase_21 AC 86 ms
55,404 KB
testcase_22 AC 79 ms
55,136 KB
testcase_23 AC 112 ms
57,372 KB
testcase_24 AC 101 ms
57,376 KB
testcase_25 AC 105 ms
57,108 KB
testcase_26 AC 70 ms
54,880 KB
testcase_27 AC 71 ms
54,872 KB
testcase_28 AC 70 ms
54,880 KB
testcase_29 AC 71 ms
54,888 KB
testcase_30 AC 71 ms
54,884 KB
testcase_31 AC 75 ms
54,984 KB
testcase_32 AC 76 ms
55,024 KB
testcase_33 AC 75 ms
55,020 KB
testcase_34 AC 88 ms
55,528 KB
testcase_35 AC 156 ms
55,468 KB
testcase_36 AC 167 ms
56,716 KB
testcase_37 AC 144 ms
57,556 KB
testcase_38 AC 160 ms
57,032 KB
testcase_39 AC 149 ms
56,680 KB
testcase_40 AC 792 ms
119,736 KB
testcase_41 AC 513 ms
114,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static List<MyWord> list;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        list = IntStream.range(0, n).mapToObj(i -> new MyWord(sc.next())).sorted().toList();
        dp = new int[n][26];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n - 1, 25));
    }
    
    static int dfw(int idx, int v) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] < 0) {
            if (v < list.get(idx).right) {
                dp[idx][v] = dfw(idx - 1, v);
            } else {
                dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx - 1, list.get(idx).left) + list.get(idx).value);
            }
        }
        return dp[idx][v];
    }
    
    static class MyWord implements Comparable<MyWord> {
        int left;
        int right;
        int value;
        
        public MyWord(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public MyWord(String s) {
            this(s.charAt(0) - 'a', s.charAt(s.length() - 1) - 'a', isGood(s) ? s.length() : 0);
        }
        
        public int compareTo(MyWord another) {
            if (right == another.right) {
                return left - another.left;
            } else {
                return right - another.right;
            }
        }
        
        private static boolean isGood(String s) {
            return IntStream.range(0, s.length() - 1).allMatch(i -> s.charAt(i) <= s.charAt(i + 1));
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0