結果

問題 No.430 文字列検索
ユーザー airisairis
提出日時 2016-10-03 21:18:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 369 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 1,712 ms
コンパイル使用メモリ 169,752 KB
実行使用メモリ 27,520 KB
最終ジャッジ日時 2024-05-03 04:30:06
合計ジャッジ時間 3,335 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 369 ms
27,520 KB
testcase_02 AC 20 ms
5,376 KB
testcase_03 AC 21 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 342 ms
27,136 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 16 ms
5,888 KB
testcase_11 AC 96 ms
8,320 KB
testcase_12 AC 101 ms
8,320 KB
testcase_13 AC 97 ms
8,576 KB
testcase_14 AC 77 ms
7,040 KB
testcase_15 AC 63 ms
6,016 KB
testcase_16 AC 26 ms
5,376 KB
testcase_17 AC 24 ms
5,376 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