結果

問題 No.430 文字列検索
ユーザー Vi24EVi24E
提出日時 2022-07-10 18:39:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,417 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 204,912 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-02 01:57:18
合計ジャッジ時間 5,915 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

template <typename T>
//aの中からbに合致するものの最初のindexのvectorを返す
vector<int> get_rolling_hash(vector<T> &a, vector<T> &b){
	using lll = __int128_t;
	const lll base = 591591593, mod = (1ll<<61) - 1, safe = 127;
	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 = (hash_b * base + (lll)x + safe) % mod;
	}

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

	vector<int> pos;
	if (hash_a == hash_b) pos.push_back(0);
	for (int i = 0; i < n - m; i++){
		hash_a = (((hash_a - basem * ((lll)(a[i]) + safe)) % mod + mod) * base + (lll)(a[i + m]) + safe) % mod;
		if (hash_a == hash_b) pos.push_back(i + 1);
	}
	return pos;
}

template <typename T>
//aの中からbに合致するものの最初のindexのvectorを返す
int rolling_hash(vector<T> &a, vector<T> &b){
	using lll = __int128_t;
	const lll base = 26, mod = (1ll<<61) - 1, safe = 0;
	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 = (hash_b * base + (lll)x + safe) % mod;
	}

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

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

vector<int> get_rolling_hash(string &s, string &t){
	vector<char> a(s.size()), b(t.size());
	for (int i = 0; i < (int)s.size(); i++){
		a[i] = s[i];
	}
	for (int i = 0; i < (int)t.size(); i++){
		b[i] = t[i];
	}
	return get_rolling_hash(a, b);
}

int rolling_hash(string &s, string &t){
	vector<char> a(s.size()), b(t.size());
	for (int i = 0; i < (int)s.size(); i++){
		a[i] = s[i];
	}
	for (int i = 0; i < (int)t.size(); i++){
		b[i] = t[i];
	}
	return rolling_hash(a, b);
}

int main(){
	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