結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  ldsyb | 
| 提出日時 | 2017-11-27 17:38:55 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 409 ms / 2,000 ms | 
| コード長 | 875 bytes | 
| コンパイル時間 | 1,523 ms | 
| コンパイル使用メモリ | 169,900 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:18:35 | 
| 合計ジャッジ時間 | 6,445 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct rolling_hash {
	string s;
	vector<uint64_t> hash, bp;
	uint64_t b = 1007;
	int n;
	
	rolling_hash(const string &s) : s(s), n(s.size()) {
		hash.resize(n + 1);
		bp.resize(n + 1);
		hash[0] = 0;
		bp[0] = 1;
		for (int i = 0; i < n; i++) {
			hash[i + 1] = hash[i] * b + s[i];
			bp[i + 1] = bp[i] * b;
		}
	}
	
	//[l, r)
	uint64_t get_hash(int l, int r) {
		assert(0 <= l && l < r && r <= n);
		return hash[r] - bp[r - l] * hash[l];
	}
};
int main() {
	string s;
	cin >> s;
	rolling_hash s_hash(s);
	int m;
	cin >> m;
	int ans = 0;
	for (int i = 0; i < m; i++) {
		string t;
		cin >> t;
		rolling_hash t_hash(t);
		uint64_t puni = t_hash.get_hash(0, t.size());
		for (int j = 0; j + t.size() <= s.size(); j++) {
			if (puni == s_hash.get_hash(j, j + t.size())) {
				ans++;
			}
		}
	}
	cout << ans << endl;
	return 0;
}
            
            
            
        