結果

問題 No.430 文字列検索
ユーザー kurenai3110kurenai3110
提出日時 2016-10-03 02:17:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,130 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 79,160 KB
実行使用メモリ 8,852 KB
最終ジャッジ日時 2024-05-03 04:29:09
合計ジャッジ時間 3,595 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 217 ms
8,852 KB
testcase_02 AC 197 ms
5,376 KB
testcase_03 AC 198 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 201 ms
5,848 KB
testcase_12 AC 197 ms
5,376 KB
testcase_13 AC 197 ms
5,376 KB
testcase_14 AC 198 ms
5,376 KB
testcase_15 AC 198 ms
5,376 KB
testcase_16 AC 198 ms
5,376 KB
testcase_17 AC 199 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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