結果

問題 No.430 文字列検索
ユーザー masamasa
提出日時 2020-02-27 02:32:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,184 bytes
コンパイル時間 856 ms
コンパイル使用メモリ 82,760 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2024-04-21 16:45:23
合計ジャッジ時間 2,254 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 179 ms
26,624 KB
testcase_02 AC 15 ms
5,376 KB
testcase_03 AC 14 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 175 ms
26,368 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 13 ms
5,760 KB
testcase_11 AC 59 ms
7,552 KB
testcase_12 AC 58 ms
7,552 KB
testcase_13 AC 59 ms
7,680 KB
testcase_14 AC 45 ms
6,016 KB
testcase_15 AC 36 ms
5,376 KB
testcase_16 AC 16 ms
5,376 KB
testcase_17 AC 15 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <map>

using namespace std;

// Aに0を割り当てるとAAAとかでおかしくなるので、1を割り当てる
const int N_CHAR = 27;
string S;
map<long long, int> hashes;

long long calc_hash(string t) {
	long long x = 0;
	for (char c: t) {
		x = x * N_CHAR + (c - 'A' + 1);
	}
	return x;
}

void make_hashes(int len) {
// printf("---make_hashes: len %d\n", len);
	if (len > S.size()) {
		return;
	}
	long long mod = 1;
	for (int i = 0; i < len; i++) {
		mod *= N_CHAR;
	}

	auto t = S.substr(0, len);
// cout << t << endl;
	auto x = calc_hash(t);
	hashes[x]++;
// printf("hash find: %15lld\n", x);
	for (int i = t.size(); i < S.size(); i++) {
		x = x * N_CHAR % mod + (S[i] - 'A' + 1);
// printf("hash find: %15lld\n", x);
		hashes[x]++;
	}
}

int main() {
	string c;
	cin >> S;

	for (int i = 1; i <= 10; i++) {
		make_hashes(i);
	}

	int ans = 0;
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> c;
		auto x = calc_hash(c);
		ans += hashes[x];
// printf("c %10s: hash %15lld, add %3d\n", c.c_str(), x, hashes[x]);
	}

	cout << ans << endl;
}
0