結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-02-28 23:39:48 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 503 ms / 2,000 ms |
| コード長 | 7,566 bytes |
| コンパイル時間 | 2,099 ms |
| コンパイル使用メモリ | 83,140 KB |
| 実行使用メモリ | 80,216 KB |
| 最終ジャッジ日時 | 2024-11-10 01:10:24 |
| 合計ジャッジ時間 | 7,317 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
import java.util.*;
import java.io.*;
import java.math.*;
import java.util.stream.*;
import java.util.function.*;
class Main implements Runnable {
public void solve() {
String s = in.next();
int n = in.nextInt() ;
String [] str = in.next(n);
RollingHash rh = new RollingHash(s);
Map<Long,Long> count = new HashMap<>();
long ans = 0 ;
for(int i = 0 ; i < s.length() ; i ++) {
for(int len = 0 ; len <= 10 ; len ++) {
if((i + len + 1) <= s.length()) {
var hash = rh.get(i , i + len + 1);
count.put(hash , count.getOrDefault(hash , 0L) + 1L);
}
}
}
for(int i = 0 ; i < n ; i ++) {
var keyHash = new RollingHash(str[i]).get(0 , str[i].length());
if(!count.containsKey(keyHash)) continue;
ans += count.get(keyHash);
}
out.print(ans);
}
// 文字列のハッシュ値前計算O(|S|)
// 範囲ハッシュ値取得O(1)
public class RollingHash {
public static final long B = (long) 1e5 + 7;
public static final long MOD = (long) 1e9 + 7;
private String S;
private int N;
private long[] Power;
private long[] Hash;
public RollingHash(String s) {
this.S = s;
this.N = s.length();
this.Power = new long[this.N + 1];
this.Power[0] = 1;
for (int i = 0; i < N; i++) {
this.Power[i + 1] = (this.Power[i] * B) % MOD;
}
this.Hash = new long[this.N + 1];
for (int i = 0; i < N; i++) {
this.Hash[i + 1] = (this.Hash[i] * B + S.charAt(i)) % MOD;
}
}
public long get(int l , int r) {
long hash = this.Hash[r] - (this.Hash[l] * this.Power[r - l] % MOD);
if (hash < 0) hash += MOD;
return hash;
}
}
public PrintWriter out = new PrintWriter(System.out);
public Input in = new Input() ;
public static final int inf = (1 << 30);
public static final long lnf = (1L << 60);
public static final String yes = "Yes" , no = "No" ;
public static final int mod7 = 1000000007 , mod9 = 998244353;
public static final int [] dy4 = {-1,0,1,0} , dx4 = {0,1,0,-1};
public static final int [] dy8 = {-1,-1,-1,0,1,1,1,0} , dx8 = {-1,0,1,1,1,0,-1,-1};
public long hashing() {
return (long) Math.ceil(Math.random() * ((1L << 60) - 1));
}
public long log(long n) {
return (long) (Math.log(n) / Math.log(2));
}
public long sqrt(long n) {
return (long) (Math.sqrt(n)) ;
}
public long perm(long n) {
long res = 1 ;
for(long i = 1 ; i <= n ; i ++) res *= i ;
return res ;
}
public long [][] combi(int n) {
int MAX = n + 1 ; long [][] com = new long[MAX][MAX];
for(int i = 0 ; i < MAX ; i ++) com[i][0] = 1;
for(int i = 1 ; i < MAX ; i ++) for(int j = 1 ; j <= i ; j ++) {
com[i][j] = com[i-1][j-1] + com[i-1][j];
}
return com ;
}
public static void main(String ... args) { new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start(); }
public void run() { solve(); out.flush(); }
}
class Input {
private final InputStream in = System.in;
private final Scanner sc = new Scanner(System.in);
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++]; else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
private boolean hasNext() {
while(hasNextByte() && !isPrintableChar(buffer[ptr])) {
ptr++;
}
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char nextChar() {
return next().charAt(0);
}
public BigInteger nextBigInteger() {
return sc.nextBigInteger();
}
public int [] nextInt(int n) {
int [] array = new int[n];
for(int i = 0 ; i < n ; i ++) {
array[i] = nextInt();
}
return array ;
}
public int [][] nextInt(int n , int m) {
int [][] array = new int[n][m];
for(int i = 0 ; i < n ; i ++) {
array[i] = nextInt(m);
}
return array ;
}
public long [] nextLong(int n) {
long [] array = new long[n];
for(int i = 0 ; i < n ; i ++) {
array[i] = nextLong();
}
return array ;
}
public long [][] nextLong(int n , int m) {
long [][] array = new long[n][m];
for(int i = 0 ; i < n ; i ++) {
array[i] = nextLong(m);
}
return array ;
}
public double [] nextDouble(int n) {
double [] array = new double[n];
for(int i = 0 ; i < n ; i ++) {
array[i] = nextDouble();
}
return array ;
}
public String [] next(int n) {
String [] array = new String[n];
for(int i = 0 ; i < n ; i ++) {
array[i] = next();
}
return array ;
}
public String [][] next(int n , int m) {
String [][] array = new String[n][m];
for(int i = 0 ; i < n ; i ++) {
array[i] = next(m);
}
return array ;
}
public char [] nextChar(int n) {
char [] array = new char[n];
String string = next() ;
for(int i = 0 ; i < n ; i ++) {
array[i] = string.charAt(i);
}
return array ;
}
public char [][] nextChar(int n , int m) {
char [][] array = new char[n][m];
for(int i = 0 ; i < n ; i ++) {
array[i] = nextChar(m);
}
return array ;
}
}