結果

問題 No.430 文字列検索
ユーザー nonpro3_shojinnonpro3_shojin
提出日時 2019-09-24 01:23:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,821 ms / 2,000 ms
コード長 2,364 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 76,832 KB
実行使用メモリ 50,276 KB
最終ジャッジ日時 2023-10-19 08:36:24
合計ジャッジ時間 14,293 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1,821 ms
50,276 KB
testcase_02 AC 886 ms
4,868 KB
testcase_03 AC 899 ms
4,868 KB
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 1,787 ms
49,748 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 112 ms
8,336 KB
testcase_11 AC 1,124 ms
11,996 KB
testcase_12 AC 1,113 ms
12,260 KB
testcase_13 AC 1,125 ms
12,260 KB
testcase_14 AC 1,033 ms
9,092 KB
testcase_15 AC 997 ms
7,244 KB
testcase_16 AC 924 ms
4,868 KB
testcase_17 AC 919 ms
4,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;

typedef long long ll;

#define Try 2

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

struct Rolling_Hash {

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

	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];
			}
		}
	}
	
	ll hash(int l, int r, int p = 0) {
		//[l, r)  pはどの基底とMODでやるか
		if (r <= l || r > XL)return -1;
		ll ret = 0;
		ret = (memo[p][r] - memo[p][l] + MD[p]) % MD[p];
		ret = (ret * modpow(modpow(base[p], l, MD[p]), MD[p] - 2, MD[p])) % MD[p];
		return ret;
	}
	bool match(const string& Y) {

		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)return true;
				Yh = (Yh * base[t]) % MD[t];
			}
		}

		return false;
	}

};

ll hash_from_str(const string& X, ll base, ll mod) {
	ll ret = 0, b = 1;
	for (int i = 0; i < X.size(); i++) {
		ret = (ret + X[i] * b) % mod;
		b = (b * base) % mod;
	}
	return ret;
}

int main() {
	string S;
	int M;
	cin >> S >> M;
	Rolling_Hash rh;
	rh.setup(S);
	map<ll, ll> Map[10][Try];
	
	for (int t = 0; t < Try; t++) {
		for (int i = 0; i < S.size(); i++) {
			for (int j = i + 1; j <= S.size() && j - i <= 10; j++) {
				Map[j - i - 1][t][rh.hash(i, j, t)]++;
			}
		}
	}
	
	ll ans = 0;
	while (M--) {
		string C;
		cin >> C;
		ll tmp = 1145141919810;
		for (int t = 0; t < Try; t++) {
			tmp = min(tmp, 
				Map[C.size() - 1][t][hash_from_str(C, rh.base[t], rh.MD[t])]);
		}
		ans += tmp;
	}
	cout << ans << endl;
	return 0;
}
0