結果

問題 No.430 文字列検索
ユーザー airisairis
提出日時 2016-10-03 21:18:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 1,091 ms
コンパイル使用メモリ 155,296 KB
実行使用メモリ 32,840 KB
最終ジャッジ日時 2023-08-15 17:35:34
合計ジャッジ時間 2,840 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
9,804 KB
testcase_01 AC 258 ms
32,840 KB
testcase_02 AC 17 ms
10,092 KB
testcase_03 AC 18 ms
10,080 KB
testcase_04 AC 3 ms
9,944 KB
testcase_05 AC 2 ms
9,720 KB
testcase_06 AC 2 ms
9,692 KB
testcase_07 AC 2 ms
9,784 KB
testcase_08 AC 254 ms
32,696 KB
testcase_09 AC 3 ms
9,792 KB
testcase_10 AC 15 ms
12,072 KB
testcase_11 AC 88 ms
13,752 KB
testcase_12 AC 87 ms
13,744 KB
testcase_13 AC 87 ms
13,928 KB
testcase_14 AC 70 ms
12,204 KB
testcase_15 AC 59 ms
11,360 KB
testcase_16 AC 22 ms
10,212 KB
testcase_17 AC 19 ms
10,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(x, to) for (int x = 0; x < (to); x++)
#define REP(x, a, to) for (int x = (a); x < (to); x++)
#define EPS (1e-14)
#define _PA(x,N) rep(i,N){cout<<x[i]<<" ";}cout<<endl;
#define _PA2(x,H,W) rep(i,(H)){rep(j,(W)){cout<<x[i][j]<<" ";}cout<<endl;}

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef complex<double> Complex;
typedef vector< vector<int> > Mat;

string S;
int M;
string C[5005];
ll ans;


struct RollingHash {
	string s;
	ull p;
	ull mod;
	ull phash[int(1e+6) + 5];
	ull pow[int(1e+6) + 5];
	void init(string t, ull q=1e9+7, ull m=(1ULL<<60)) {
		s = t;
		p = q;
		mod = m;
		//memset(phash, 0, sizeof(phash));
		//memset(pow, 0, sizeof(pow));
	}
	void build() {
		phash[0] = 0; //1-indexed
		pow[0] = 1;// 0-indexed
		for (int i = 0; i < (int)s.size(); i++) {
			phash[i + 1] = s[i] + (phash[i] * p) % mod;
			phash[i + 1] %= mod;
			pow[i + 1] = p * pow[i];
			pow[i + 1] %= mod;
		}
	}
	ull hash(int i, int j) {
		ull res = (phash[i] * pow[j - i]) % mod;
		return (phash[j] + mod - res) % mod;
	}
	size_t size() {
		return s.size();
	}
};


RollingHash rhs;
RollingHash rhq;

map<ull, ll> freq;

void solve() {
	rhs.init(S);
	rhs.build();

	for (int i = 0; i < S.size(); i++) {
		for (int len = 1; len <= 10 && i + len <= S.size(); len++) {
			freq[rhs.hash(i, i + len)] += 1;
		}
	}

	for (int i = 0; i < M; i++) {
		rhq.init(C[i]);
		rhq.build();
		ull hash1 = rhq.hash(0, C[i].size());
		ans += freq[hash1];
	}

	cout << ans << endl;
}

int main() {
	cin >> S;
	cin >> M;
	for (int i = 0; i < M; i++) {
		cin >> C[i];
	}
	solve();
	return 0;
}


0