結果

問題 No.430 文字列検索
ユーザー sugim48sugim48
提出日時 2016-10-02 22:35:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,798 bytes
コンパイル時間 1,140 ms
コンパイル使用メモリ 111,652 KB
実行使用メモリ 20,880 KB
最終ジャッジ日時 2023-08-13 18:26:54
合計ジャッジ時間 2,748 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 163 ms
20,880 KB
testcase_02 AC 24 ms
4,376 KB
testcase_03 AC 24 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 153 ms
20,548 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 12 ms
5,176 KB
testcase_11 AC 68 ms
6,576 KB
testcase_12 AC 68 ms
6,536 KB
testcase_13 AC 69 ms
6,620 KB
testcase_14 AC 57 ms
5,564 KB
testcase_15 AC 49 ms
4,680 KB
testcase_16 AC 27 ms
4,380 KB
testcase_17 AC 25 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#include <random>
#include <list>
using namespace std;
 
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };
 
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define pb push_back
 
int INF = INT_MAX / 10;
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int mod_inv(ll x, int M) {
	int t = M - 2, y = 1;
	for (; t; t>>=1) {
		if (t & 1) y = y * x % M;
		x = x * x % M;
	}
	return y;
}

struct rolling_hash {
	int M;
	vector<int> a, b;
	rolling_hash(string s, int _M) {
		M = _M;
		int N = s.length();
		a.resize(N + 1);
		ll x = 1;
		for (int i = 0; i < N; i++) {
			a[i + 1] = (a[i] + x * s[i]) % M;
			x = x * 256 % M;
		}
		ll inv = mod_inv(256, M);
		b.resize(N + 1);
		b[0] = 1;
		for (int i = 0; i < N; i++)
			b[i + 1] = b[i] * inv % M;
	}
	int get(int l, int r) {
		return ((ll)(a[r] - a[l]) * b[l] % M + M) % M;
	}
};

int main() {
	string s; cin >> s;
	int N = s.length();
	rolling_hash a(s, _MOD);
	vector<map<int, int> > mp(11);
	for (int d = 1; d <= 10; d++)
		for (int l = 0; l + d <= N; l++)
			mp[d][a.get(l, l + d)]++;
	int M; cin >> M;
	ll ans = 0;
	while (M--) {
		string t; cin >> t;
		rolling_hash b(t, _MOD);
		ans += mp[t.length()][b.get(0, t.length())];
	}
	cout << ans << endl;
}
0