結果

問題 No.1512 作文
ユーザー tentententen
提出日時 2022-09-30 12:00:55
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,320 bytes
コンパイル時間 3,618 ms
コンパイル使用メモリ 76,120 KB
実行使用メモリ 121,844 KB
最終ジャッジ日時 2023-08-24 05:17:02
合計ジャッジ時間 11,323 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,476 KB
testcase_01 AC 44 ms
49,476 KB
testcase_02 AC 42 ms
49,456 KB
testcase_03 AC 42 ms
47,940 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 118 ms
55,160 KB
testcase_10 AC 123 ms
56,056 KB
testcase_11 AC 123 ms
56,064 KB
testcase_12 AC 118 ms
55,696 KB
testcase_13 AC 125 ms
55,352 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 61 ms
52,068 KB
testcase_22 AC 47 ms
49,752 KB
testcase_23 AC 62 ms
52,080 KB
testcase_24 AC 78 ms
52,012 KB
testcase_25 AC 63 ms
51,860 KB
testcase_26 AC 44 ms
49,676 KB
testcase_27 AC 45 ms
49,460 KB
testcase_28 AC 47 ms
49,452 KB
testcase_29 AC 45 ms
49,468 KB
testcase_30 AC 46 ms
49,464 KB
testcase_31 AC 47 ms
47,656 KB
testcase_32 AC 46 ms
49,812 KB
testcase_33 AC 47 ms
49,744 KB
testcase_34 AC 57 ms
50,648 KB
testcase_35 AC 117 ms
52,748 KB
testcase_36 AC 119 ms
52,860 KB
testcase_37 AC 105 ms
52,344 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 726 ms
120,692 KB
testcase_41 AC 403 ms
121,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<Unit> units = 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++) {
            Unit x = new Unit(sc.next());
            if (x.isSorted) {
                units.add(x);
            }
        }
        Collections.sort(units);
        dp = new int[units.size()][26];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(units.size() - 1, 25));
    }
    
    static int dfw(int idx, int c) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][c] < 0) {
            dp[idx][c] = dfw(idx - 1, c);
            if (units.get(idx).right <= c) {
                dp[idx][c] = Math.max(dp[idx][c], dfw(idx - 1, units.get(idx).left) + units.get(idx).length);
            }
        }
        return dp[idx][c];
    }
    
    static class Unit implements Comparable<Unit> {
        int left;
        int right;
        int length;
        boolean isSorted;
        
        public Unit(String s) {
            this.left = s.charAt(0) - 'a';
            this.right = s.charAt(s.length() - 1) - 'a';
            char[] tmp = s.toCharArray();
            Arrays.sort(tmp);
            isSorted = s.equals(new String(tmp));
            length = s.length();
        }
        
        public int compareTo(Unit another) {
            return right - another.right;
        }
    }
}
    
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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0