結果
| 問題 |
No.1512 作文
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-05-25 11:06:12 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 886 ms / 2,000 ms |
| コード長 | 2,223 bytes |
| コンパイル時間 | 3,635 ms |
| コンパイル使用メモリ | 78,836 KB |
| 実行使用メモリ | 130,428 KB |
| 最終ジャッジ日時 | 2024-10-13 19:25:04 |
| 合計ジャッジ時間 | 12,284 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 38 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<String> list = 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++) {
String s = sc.next();
if (check(s)) {
list.add(s);
}
}
Collections.sort(list, new Comparator<String>() {
public int compare(String s1, String s2) {
if (s1.charAt(s1.length() - 1) == s2.charAt(s2.length() - 1)) {
return s1.charAt(0) - s2.charAt(0);
} else {
return s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1);
}
}
});
dp = new int[list.size()][26];
System.out.println(dfw(list.size() - 1, 25));
}
static boolean check(String s) {
char start = 'a';
for (char c : s.toCharArray()) {
if (c < start) {
return false;
}
start = c;
}
return true;
}
static int dfw(int idx, int alpha) {
if (idx < 0) {
return 0;
}
if (dp[idx][alpha] == 0) {
String s = list.get(idx);
if (s.charAt(s.length() - 1) - 'a' > alpha) {
dp[idx][alpha] = dfw(idx - 1, alpha);
} else {
dp[idx][alpha] = Math.max(dfw(idx - 1, alpha), dfw(idx - 1, s.charAt(0) - 'a') + s.length());
}
}
return dp[idx][alpha];
}
}
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 String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten