結果

問題 No.430 文字列検索
ユーザー kurenai3110
提出日時 2016-10-03 02:17:22
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,130 bytes
コンパイル時間 725 ms
コンパイル使用メモリ 79,028 KB
実行使用メモリ 8,984 KB
最終ジャッジ日時 2024-11-10 00:08:05
合計ジャッジ時間 3,096 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;

string s,target;
bool fin[50001];
vector<long long>Hash[50001];
int rolling_hash(int size, long long val) {
	int cnt = 0;
	if (fin[size]) {
		for (int i = 0; i < Hash[size].size(); i++) {
			if (Hash[size][i] == val)cnt++;
		}
	}
	else {
		long long hash = 0;
		for (int j = 0; j < size; j++) hash += (s[j] - 'A')*pow(26, j);
		if (hash == val)cnt++;
		Hash[size].push_back(hash);
		for (int j = size; j < s.size(); j++) {
			hash -= s[j - size] - 'A';
		    hash /= 26;
			hash += (s[j] - 'A')*pow(26, size - 1);
			if (hash == val)cnt++;
			Hash[size].push_back(hash);
		}
		fin[size] = true;
	}
	return cnt;
}

int main()
{

	cin >> s;
	int m; cin >> m;
	int ans = 0;

	for (int i = 0; i < m; i++) {
		cin >> target;
		if (target.size() > s.size())continue;
		long long target_hash=0;
		for (int j = 0; j < target.size(); j++) target_hash += (target[j] - 'A')*pow(26, j);
		ans += rolling_hash(target.size(), target_hash);
	}

	cout << ans << endl;

	return 0;
}
0