結果

問題 No.430 文字列検索
ユーザー masamasa
提出日時 2020-02-27 02:31:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,169 bytes
コンパイル時間 1,159 ms
コンパイル使用メモリ 83,016 KB
実行使用メモリ 26,752 KB
最終ジャッジ日時 2024-04-21 16:45:20
合計ジャッジ時間 3,794 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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