結果

問題 No.430 文字列検索
ユーザー buno15buno15
提出日時 2021-10-25 21:46:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 449 ms / 2,000 ms
コード長 5,130 bytes
コンパイル時間 2,865 ms
コンパイル使用メモリ 87,744 KB
実行使用メモリ 90,324 KB
最終ジャッジ日時 2024-08-15 16:36:19
合計ジャッジ時間 7,028 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 208 ms
56,644 KB
testcase_01 AC 47 ms
50,284 KB
testcase_02 AC 449 ms
90,324 KB
testcase_03 AC 217 ms
56,232 KB
testcase_04 AC 208 ms
56,468 KB
testcase_05 AC 46 ms
50,184 KB
testcase_06 AC 48 ms
50,076 KB
testcase_07 AC 46 ms
49,752 KB
testcase_08 AC 45 ms
50,076 KB
testcase_09 AC 307 ms
82,364 KB
testcase_10 AC 48 ms
50,092 KB
testcase_11 AC 101 ms
53,848 KB
testcase_12 AC 245 ms
62,176 KB
testcase_13 AC 244 ms
62,596 KB
testcase_14 AC 240 ms
62,392 KB
testcase_15 AC 230 ms
61,776 KB
testcase_16 AC 224 ms
60,040 KB
testcase_17 AC 205 ms
58,500 KB
testcase_18 AC 215 ms
57,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main {

	static final int INF = 1000000000;
	static final long MOD = 1000000007;
	static final double EPS = 1e-10;

	public static void main(String args[]) throws IOException {
		IO io = new IO();

		char s[] = io.getCharArr();

		int m = io.getInt();

		RollingHash rh = new RollingHash(s);

		Map<RollingHash.Pair, Long> map = new HashMap<>();

		for (int i = 1; i <= 10; i++) {
			for (int j = 0; j < s.length; j++) {
				if (j + i > s.length)
					break;

				RollingHash.Pair p = rh.get(j, j + i);
				if (map.containsKey(p)) {
					map.put(p, map.get(p) + 1);
				} else {
					map.put(p, 1L);
				}
			}
		}

		int ans = 0;

		for (int i = 0; i < m; i++) {
			char c[] = io.getCharArr();
			RollingHash rh2 = new RollingHash(c);

			RollingHash.Pair p = rh2.get(0, c.length);

			if (map.containsKey(p))
				ans += map.get(p);
		}

		System.out.println(ans);

	}
}

class RollingHash {
	final long base1 = 1007, base2 = 2009;
	final long MOD1 = 1000000007, MOD2 = 1000000009;
	long hash1[], hash2[], power1[], power2[];

	public RollingHash(char str[]) {
		int n = (int) str.length;

		hash1 = new long[n + 1];
		hash2 = new long[n + 1];
		power1 = new long[n + 1];
		power2 = new long[n + 1];

		power1[0] = 1;
		power2[0] = 1;

		for (int i = 0; i < n; i++) {
			hash1[i + 1] = (hash1[i] * base1 + str[i]) % MOD1;
			hash2[i + 1] = (hash2[i] * base2 + str[i]) % MOD2;
			power1[i + 1] = (power1[i] * base1) % MOD1;
			power2[i + 1] = (power2[i] * base2) % MOD2;
		}
	}

	public Pair get(int l, int r) {
		long res1 = hash1[r] - hash1[l] * power1[r - l] % MOD1;
		if (res1 < 0)
			res1 += MOD1;
		long res2 = hash2[r] - hash2[l] * power2[r - l] % MOD2;
		if (res2 < 0)
			res2 += MOD2;
		return new Pair(res1, res2);
	}

	class Pair {
		long a;
		long b;

		public Pair(long a, long b) {
			this.a = a;
			this.b = b;
		}

		@Override
		public boolean equals(Object o) {
			Pair p = (Pair) o;
			if (this.a == p.a && this.b == p.b) {
				return true;
			} else {
				return false;
			}
		}

		@Override
		public int hashCode() {
			return (int) (a + b);
		}
	}
}

class IO {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public IO() {

	}

	public void println(String str) {
		System.out.println(str);
	}

	public void printArr(Object o[]) {
		for (int i = 0; i < o.length; i++) {
			System.out.print(o + " ");
		}
		System.out.println();
	}

	public int getInt() throws IOException {
		return Integer.parseInt(br.readLine());
	}

	public long getLong() throws IOException {
		return Long.parseLong(br.readLine());
	}

	public double getDouble() throws IOException {
		return Double.parseDouble(br.readLine());
	}

	public String getLine() throws IOException {
		return br.readLine();
	}

	public int[] getIntArrPrim() throws IOException {
		String str[] = br.readLine().split(" ");
		int a[] = new int[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		return a;
	}

	public Integer[] getIntArr() throws IOException {
		String str[] = br.readLine().split(" ");
		Integer a[] = new Integer[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		return a;
	}

	public Long[] getLongArr() throws IOException {
		String str[] = br.readLine().split(" ");
		Long a[] = new Long[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Long.parseLong(str[i]);
		}

		return a;
	}

	public long[] getLongArrPrim() throws IOException {
		String str[] = br.readLine().split(" ");
		long a[] = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Long.parseLong(str[i]);
		}

		return a;
	}

	public String[] getStrArr(String split) throws IOException {
		return br.readLine().split(split);
	}

	public char[] getCharArr() throws IOException {
		return br.readLine().toCharArray();
	}

	public int[][] getIntMap(int w, int h, String split) throws IOException {
		int a[][] = new int[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Integer.parseInt(str[j]);
			}
		}

		return a;
	}

	public long[][] getLongMap(int w, int h, String split) throws IOException {
		long a[][] = new long[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Long.parseLong(str[j]);
			}
		}

		return a;
	}

	public double[][] getDoubleMap(int w, int h, String split) throws IOException {
		double a[][] = new double[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Double.parseDouble(str[j]);
			}
		}

		return a;
	}

	public char[][] getCharMap(int w, int h, String split) throws IOException {
		char a[][] = new char[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = str[j].charAt(0);
			}
		}

		return a;
	}
}
0