結果

問題 No.430 文字列検索
ユーザー Vi24EVi24E
提出日時 2022-07-10 18:10:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 837 ms / 2,000 ms
コード長 1,023 bytes
コンパイル時間 6,318 ms
コンパイル使用メモリ 198,620 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-02 01:32:03
合計ジャッジ時間 11,645 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 834 ms
4,500 KB
testcase_02 AC 834 ms
4,380 KB
testcase_03 AC 835 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 834 ms
4,380 KB
testcase_12 AC 835 ms
4,376 KB
testcase_13 AC 835 ms
4,376 KB
testcase_14 AC 837 ms
4,380 KB
testcase_15 AC 834 ms
4,380 KB
testcase_16 AC 833 ms
4,376 KB
testcase_17 AC 833 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//aの中からbに合致するものの最初のindexのvectorを返す
int rolling_hash(string &a, string &b){
	using lll = __int128_t;
	const lll base = 26, mod = (1ll<<61) - 1;
	lll basem = 1;
	int n = a.size(), m = b.size();

	lll t = m, u = base;
	t--;
    while (t) {
        if (t & 1) basem = (basem * u) % mod;
        u = (u * u) % mod;
        t >>= 1;
    }

	lll hash_a = 0, hash_b = 0;
	for (auto x : b){
		hash_b *= base;
		hash_b += (lll)(x);
	}

	for (int i = 0; i < m; i++){
		hash_a *= base;
		hash_a += (lll)(a[i]);
	}

	int pos = 0;
	if (hash_a == hash_b) pos++;
	for (int i = 0; i < n - m; i++){
		hash_a -= basem * (lll)(a[i]);
		hash_a *= base;
		hash_a += (lll)(a[i + m]);
		if (hash_a == hash_b) pos++;
	}
	return pos;
}

int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
	string s;
	cin >> s;
	int m;
	cin >> m;
	int ans = 0;
	for (int i = 0; i < m; i++){
		string c;
		cin >> c;
		ans += rolling_hash(s, c);
	}
	cout << ans << endl;
}
0