結果

問題 No.430 文字列検索
ユーザー airis
提出日時 2016-10-03 21:18:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 289 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 1,237 ms
コンパイル使用メモリ 168,336 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2024-11-10 00:08:34
合計ジャッジ時間 2,816 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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