結果

問題 No.1512 作文
ユーザー tentententen
提出日時 2024-04-23 17:55:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 2,721 bytes
コンパイル時間 3,752 ms
コンパイル使用メモリ 80,740 KB
実行使用メモリ 96,040 KB
最終ジャッジ日時 2024-04-23 17:56:05
合計ジャッジ時間 10,246 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
37,452 KB
testcase_01 AC 46 ms
37,092 KB
testcase_02 AC 46 ms
37,300 KB
testcase_03 AC 48 ms
37,192 KB
testcase_04 AC 429 ms
71,868 KB
testcase_05 AC 454 ms
72,064 KB
testcase_06 AC 449 ms
71,928 KB
testcase_07 AC 428 ms
71,956 KB
testcase_08 AC 457 ms
71,812 KB
testcase_09 AC 93 ms
39,352 KB
testcase_10 AC 92 ms
38,932 KB
testcase_11 AC 96 ms
38,616 KB
testcase_12 AC 95 ms
38,952 KB
testcase_13 AC 91 ms
38,972 KB
testcase_14 AC 130 ms
41,848 KB
testcase_15 AC 122 ms
41,516 KB
testcase_16 AC 122 ms
41,952 KB
testcase_17 AC 133 ms
41,572 KB
testcase_18 AC 132 ms
41,932 KB
testcase_19 AC 121 ms
41,820 KB
testcase_20 AC 120 ms
41,548 KB
testcase_21 AC 48 ms
37,092 KB
testcase_22 AC 46 ms
37,224 KB
testcase_23 AC 50 ms
37,364 KB
testcase_24 AC 52 ms
37,448 KB
testcase_25 AC 51 ms
37,068 KB
testcase_26 AC 45 ms
36,980 KB
testcase_27 AC 45 ms
37,268 KB
testcase_28 AC 46 ms
36,796 KB
testcase_29 AC 44 ms
37,448 KB
testcase_30 AC 44 ms
37,216 KB
testcase_31 AC 48 ms
36,924 KB
testcase_32 AC 51 ms
36,880 KB
testcase_33 AC 48 ms
37,024 KB
testcase_34 AC 61 ms
37,456 KB
testcase_35 AC 100 ms
39,756 KB
testcase_36 AC 98 ms
39,676 KB
testcase_37 AC 76 ms
38,764 KB
testcase_38 AC 117 ms
39,564 KB
testcase_39 AC 116 ms
39,628 KB
testcase_40 AC 551 ms
96,040 KB
testcase_41 AC 328 ms
86,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static List<MyString> values = new ArrayList<>();
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        while (n-- > 0) {
            String s = sc.next();
            if (isGood(s)) {
                values.add(new MyString(s));
            }
        }
        Collections.sort(values);
        dp = new int[values.size()][26];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(values.size() - 1, 25));
    }
    
    static int dfw(int idx, int v) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] < 0) {
            if (v >= values.get(idx).right) {
                dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx - 1, values.get(idx).left) + values.get(idx).length);
            } else {
                dp[idx][v] = dfw(idx - 1, v);
            }
        }
        return dp[idx][v];
    } 
    
    static boolean isGood(String s) {
        for (int i = 0; i < s.length() - 1; i++) {
            if (s.charAt(i) > s.charAt(i + 1)) {
                return false;
            }
        }
        return true;
    }
    
    static class MyString implements Comparable<MyString> {
        int left;
        int right;
        int length;
        
        public MyString(String s) {
            length = s.length();
            left = s.charAt(0) - 'a';
            right = s.charAt(length - 1) - 'a';
        }
        
        public int compareTo(MyString another) {
            if (right == another.right) {
                return left - another.left;
            } else {
                return right - another.right;
            }
        }
    }
}
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