結果
問題 |
No.1512 作文
|
ユーザー |
![]() |
提出日時 | 2022-08-05 12:48:06 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 901 ms / 2,000 ms |
コード長 | 2,702 bytes |
コンパイル時間 | 2,211 ms |
コンパイル使用メモリ | 79,092 KB |
実行使用メモリ | 136,220 KB |
最終ジャッジ日時 | 2024-09-15 08:32:26 |
合計ジャッジ時間 | 11,936 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 38 |
ソースコード
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(); } }