結果

問題 No.430 文字列検索
ユーザー airisairis
提出日時 2016-10-03 21:07:33
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,688 bytes
コンパイル時間 3,311 ms
コンパイル使用メモリ 155,400 KB
実行使用メモリ 33,116 KB
最終ジャッジ日時 2023-08-13 18:47:31
合計ジャッジ時間 3,645 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,648 KB
testcase_01 WA -
testcase_02 AC 20 ms
10,136 KB
testcase_03 AC 20 ms
10,144 KB
testcase_04 AC 3 ms
9,660 KB
testcase_05 AC 3 ms
9,712 KB
testcase_06 AC 3 ms
9,768 KB
testcase_07 AC 3 ms
9,720 KB
testcase_08 AC 403 ms
32,580 KB
testcase_09 AC 3 ms
9,756 KB
testcase_10 AC 18 ms
12,040 KB
testcase_11 AC 113 ms
13,684 KB
testcase_12 AC 110 ms
13,796 KB
testcase_13 AC 111 ms
13,816 KB
testcase_14 AC 86 ms
12,240 KB
testcase_15 AC 67 ms
11,368 KB
testcase_16 AC 25 ms
10,140 KB
testcase_17 AC 23 ms
10,088 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=1e+9 + 7, ull m=(1ULL<<30)) {
		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]-'0') + phash[i] * p;
			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