結果

問題 No.430 文字列検索
ユーザー nonpro3_shojinnonpro3_shojin
提出日時 2019-09-24 00:33:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,055 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 74,700 KB
実行使用メモリ 24,080 KB
最終ジャッジ日時 2023-10-19 08:34:56
合計ジャッジ時間 2,842 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 244 ms
24,080 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 241 ms
23,816 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 19 ms
6,056 KB
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<string>
#include<vector>
#include<map>

using namespace std;

typedef long long ll;

#define Try 1

struct Rolling_Hash {

	const ll MD[Try] = { 999999937 };
	const ll base[Try] = { 313 };
	string X;//Xの連続部分列の中にYがあるかをチェック
	vector<ll> memo[Try];
	int XL;
	int f[100000] = {};

	void setup(const string& a = "") {
		X = a;
		for (int t = 0; t < Try; t++) {
			memo[t].resize(X.size() + 1);
			XL = X.size();
			//(0, i]のハッシュ値の計算をする 累積和的にやる
			ll powbase = 1;
			memo[t][1] = X[0];
			for (int i = 2; i <= XL; i++) {
				powbase *= base[t], powbase %= MD[t];
				memo[t][i] = (memo[t][i - 1] + powbase * X[i - 1]) % MD[t];
			}
		}
	}

	int match(const string& Y) {

		int ret = 0;
		int YL = Y.size();
		for (int t = 0; t < Try; t++) {
			ll Yh = 0;
			ll powb = 1;
			for (int i = 0; i < YL; i++) {
				Yh = (Yh + Y[i] * powb) % MD[t];
				powb *= base[t], powb %= MD[t];
			}
			for (int i = 0; i + YL <= XL; i++) {
				ll Xh = (memo[t][i + YL] - memo[t][i] + MD[t]) % MD[t];
				if (Xh == Yh)ret++;
				Yh = (Yh * base[t]) % MD[t];
			}
		}

		return ret;
	}

};

ll modpow(ll a, ll b) {
	ll ret = 1, kakeru = a;
	while (b > 0) {
		if (b & 1) {
			ret *= kakeru, ret %= 999999937;
		}
		kakeru *= kakeru, kakeru %= 999999937;
		b >>= 1;
	}
	return ret;
}

int main() {

	string S;
	int M;
	cin >> S >> M;
	Rolling_Hash rh;
	rh.setup(S);

	map<ll, ll> Map[10];
	for (int i = 1; i <= 9; i++) {
		ll base = 1;
		for (int j = 0; j + i <= S.size(); j++) {
			ll tmp = (rh.memo[0][j + i] - rh.memo[0][j] + 999999937 ) % 999999937;
			tmp = (tmp * modpow(base, 999999937 - 2)) % 999999937;
			base *= 313, base %= 999999937;
			Map[i - 1][tmp]++;
		}
	}

	ll ans = 0;

	for (int i = 0; i < M; i++) {
		string C;
		cin >> C;
		ll hash = 0, base = 1;
		for (int j = 0; j < C.size(); j++) {
			hash += (C[j] * base % 999999937), hash %= 999999937;
			base *= 313, base %= 999999937;
		}

		ans += Map[C.size() - 1][hash];
	}
	cout << ans << endl;

	return 0;
}

0