結果
| 問題 | No.430 文字列検索 |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-11 16:55:44 |
| 言語 | Java (openjdk 25.0.1) |
| 結果 |
AC
|
| 実行時間 | 366 ms / 2,000 ms |
| コード長 | 2,141 bytes |
| 記録 | |
| コンパイル時間 | 2,519 ms |
| コンパイル使用メモリ | 78,488 KB |
| 実行使用メモリ | 73,504 KB |
| 最終ジャッジ日時 | 2024-11-10 00:58:10 |
| 合計ジャッジ時間 | 6,218 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
char[] value = sc.next().toCharArray();
int length = value.length;
long[][] hashes = new long[10][length];
ArrayList<HashMap<Long, Integer>> counts = new ArrayList<>();
for (int i = 0; i < 10; i++) {
counts.add(new HashMap<>());
}
for (int i = 0; i < length; i++) {
hashes[0][i] = value[i] - 'A';
counts.get(0).put(hashes[0][i], counts.get(0).getOrDefault(hashes[0][i], 0) + 1);
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < length - i; j++) {
hashes[i][j] = hashes[i - 1][j] * 26 + hashes[0][j + i];
counts.get(i).put(hashes[i][j], counts.get(i).getOrDefault(hashes[i][j], 0) + 1);
}
}
int m = sc.nextInt();
int ans = 0;
for (int i = 0; i < m; i++) {
String s = sc.next();
ans += counts.get(s.length() - 1).getOrDefault(getHash(s), 0);
}
System.out.println(ans);
}
static long getHash(String s) {
long hash = 0;
for (char c : s.toCharArray()) {
hash *= 26;
hash += c - 'A';
}
return hash;
}
}
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 nextLine() throws Exception {
return br.readLine();
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten