結果

問題 No.430 文字列検索
ユーザー nonpro3_shojinnonpro3_shojin
提出日時 2019-09-23 17:49:07
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,504 bytes
コンパイル時間 549 ms
コンパイル使用メモリ 66,300 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 08:13:47
合計ジャッジ時間 1,345 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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<string>
#include<vector>
#include<map>

using namespace std;

typedef long long ll;

#define Try 1
//デフォルトで3回、異なるBaseやMODで試行すること
/*
struct Rolling_Hash {

	const ll MD[Try] = {1000000007, 999999937, 943759273 };
	const ll base[Try] = {109, 313, 1289};
	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];
			}
		}
	}

	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;
	}
	
};
*/
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;
	}

};

int main() {
	
	string S;
	int M;
	cin >> S >> M;
	Rolling_Hash rh;
	rh.setup(S);
	ll ans = 0;
	/*
	for (int i = 0; i < M; i++) {
		string C;
		cin >> C;
		ans += rh.match(C);
	}
	cout << ans << endl;
	*/
	return 0;
}
0