結果

問題 No.1512 作文
ユーザー tentententen
提出日時 2022-08-05 12:48:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 856 ms / 2,000 ms
コード長 2,702 bytes
コンパイル時間 2,616 ms
コンパイル使用メモリ 75,676 KB
実行使用メモリ 136,772 KB
最終ジャッジ日時 2023-10-13 11:33:56
合計ジャッジ時間 13,998 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,680 KB
testcase_01 AC 44 ms
49,404 KB
testcase_02 AC 43 ms
49,492 KB
testcase_03 AC 43 ms
49,632 KB
testcase_04 AC 562 ms
94,324 KB
testcase_05 AC 603 ms
92,660 KB
testcase_06 AC 617 ms
92,504 KB
testcase_07 AC 596 ms
92,644 KB
testcase_08 AC 605 ms
93,008 KB
testcase_09 AC 99 ms
52,748 KB
testcase_10 AC 99 ms
52,624 KB
testcase_11 AC 100 ms
52,728 KB
testcase_12 AC 99 ms
52,528 KB
testcase_13 AC 99 ms
52,368 KB
testcase_14 AC 138 ms
55,560 KB
testcase_15 AC 129 ms
55,884 KB
testcase_16 AC 128 ms
55,796 KB
testcase_17 AC 139 ms
55,664 KB
testcase_18 AC 140 ms
55,892 KB
testcase_19 AC 131 ms
55,732 KB
testcase_20 AC 140 ms
57,492 KB
testcase_21 AC 51 ms
49,468 KB
testcase_22 AC 48 ms
49,660 KB
testcase_23 AC 54 ms
50,448 KB
testcase_24 AC 59 ms
51,300 KB
testcase_25 AC 55 ms
50,812 KB
testcase_26 AC 44 ms
49,528 KB
testcase_27 AC 43 ms
49,472 KB
testcase_28 AC 44 ms
49,380 KB
testcase_29 AC 44 ms
49,500 KB
testcase_30 AC 45 ms
49,432 KB
testcase_31 AC 46 ms
49,724 KB
testcase_32 AC 47 ms
49,556 KB
testcase_33 AC 45 ms
49,452 KB
testcase_34 AC 53 ms
49,924 KB
testcase_35 AC 100 ms
52,528 KB
testcase_36 AC 105 ms
53,160 KB
testcase_37 AC 94 ms
52,504 KB
testcase_38 AC 109 ms
52,232 KB
testcase_39 AC 109 ms
52,828 KB
testcase_40 AC 856 ms
136,772 KB
testcase_41 AC 605 ms
121,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<Word> words = new ArrayList<>();
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        while (n-- > 0) {
            Word tmp = new Word(sc.next());
            if (tmp.isGood()) {
                words.add(tmp);
            }
        }
        Collections.sort(words);
        dp = new int[words.size()][26];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(words.size() - 1, 25));
    }
    
    static int dfw(int idx, int c) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][c] < 0) {
            Word current = words.get(idx);
            if (current.right <= c) {
                dp[idx][c] = Math.max(dfw(idx - 1, c), dfw(idx - 1, current.left) + current.length());
            } else {
                dp[idx][c] = dfw(idx - 1, c);
            }
        }
        return dp[idx][c];
    }
    
    static class Word implements Comparable<Word> {
        String name;
        int left;
        int right;
        boolean good;
        
        public Word(String name) {
            this.name = name;
            char prev = 'a';
            good = true;
            for (char c : name.toCharArray()) {
                if (prev > c) {
                    good = false;
                    break;
                }
                prev = c;
            }
            left = name.charAt(0) - 'a';
            right = prev - 'a';
        }
        
        public int compareTo(Word another) {
            if (right == another.right) {
                return left - another.left;
            } else {
                return right - another.right;
            }
        }
        
        public int length() {
            return name.length();
        }
        
        public boolean isGood() {
            return good;
        }
    }
}

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 double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0