結果

問題 No.1512 作文
ユーザー tenten
提出日時 2022-09-30 12:00:55
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,320 bytes
コンパイル時間 2,595 ms
コンパイル使用メモリ 79,532 KB
実行使用メモリ 110,844 KB
最終ジャッジ日時 2024-12-22 18:43:59
合計ジャッジ時間 12,364 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 24 WA * 14
権限があれば一括ダウンロードができます

ソースコード

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