結果

問題 No.430 文字列検索
ユーザー 👑 jupirojupiro
提出日時 2019-08-15 17:42:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 2,534 bytes
コンパイル時間 2,295 ms
コンパイル使用メモリ 104,380 KB
実行使用メモリ 27,864 KB
最終ジャッジ日時 2023-10-19 19:12:55
合計ジャッジ時間 2,950 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 200 ms
27,864 KB
testcase_02 AC 34 ms
5,160 KB
testcase_03 AC 35 ms
5,160 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 190 ms
27,600 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 15 ms
6,108 KB
testcase_11 AC 81 ms
8,856 KB
testcase_12 AC 81 ms
8,856 KB
testcase_13 AC 81 ms
8,856 KB
testcase_14 AC 68 ms
7,272 KB
testcase_15 AC 58 ms
6,480 KB
testcase_16 AC 38 ms
5,424 KB
testcase_17 AC 37 ms
5,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <fstream>
#include <bitset>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long INF = 1LL << 60;
const long long MOD = 1000000007LL;
const long long MAX = 500000LL;
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;

class RollingHash {
public:
	typedef long long int_type;
	typedef pair<int_type, int_type> hash_type;

	int_type base1;
	int_type base2;
	int_type mod1;
	int_type mod2;
	vector<int_type> hash1;
	vector<int_type> hash2;
	vector<int_type> pow1;
	vector<int_type> pow2;
	RollingHash() : base1(1009), base2(1007), mod1(1000000007), mod2(1000000009) {}
	void init(const string &s) {
		ll n = s.size();
		hash1.assign(n + 1, 0);
		hash2.assign(n + 1, 0);
		pow1.assign(n + 1, 1);
		pow2.assign(n + 1, 1);
		for (ll i = 0; i < n; i++) {
			hash1[i + 1] = (hash1[i] * base1 + s[i]) % mod1;
			hash2[i + 1] = (hash2[i] * base2 + s[i]) % mod2;
			pow1[i + 1] = pow1[i] * base1 % mod1;
			pow2[i + 1] = pow2[i] * base2 % mod2;
		}
	}

	hash_type get(ll l, ll r) {
		int_type t1 = ((hash1[r] - hash1[l] * pow1[r - l]) % mod1 + mod1) % mod1;
		int_type t2 = ((hash2[r] - hash2[l] * pow2[r - l]) % mod2 + mod2) % mod2;
		return make_pair(t1, t2);
	}

	void add(string &t) {
		ll old_len = hash1.size();
		hash1.resize(old_len + t.size());
		hash2.resize(old_len + t.size());
		pow1.resize(hash1.size());
		pow2.resize(hash1.size());
		for (ll i = 0; i < t.size(); i++) {
			hash1[old_len + i] = (hash1[old_len + i - 1] * base1 + t[i]) % mod1;
			hash2[old_len + i] = (hash2[old_len + i - 1] * base2 + t[i]) % mod2;
			pow1[old_len + i] = pow1[old_len + i - 1] * base1 % mod1;
			pow2[old_len + i] = pow2[old_len + i - 1] * base2 % mod2;
		}
	}
};

int main()
{
	string s;
	cin >> s;
	RollingHash rol1;
	rol1.init(s);
	ll M;
	cin >> M;
	ll cnt = 0;
	map<pair<ll, ll>, ll> vm[11];
	for (ll i = 1; i <= 10; i++) {
		//cout << i << endl;
		for (ll j = 0; j <= (ll)s.size() - i; j++) {
			vm[i][rol1.get(j, j + i)]++;
		}
	}
	for (ll i = 0; i < M; i++) {
		string c;
		cin >> c;
		RollingHash rol2;
		rol2.init(c);
		cnt += vm[c.size()][rol2.get(0, c.size())];
	}

	cout << cnt << endl;
	return 0;
}
0