結果

問題 No.430 文字列検索
ユーザー tashikanitashikani
提出日時 2016-10-03 02:04:31
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,093 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 94,360 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2024-05-01 11:11:28
合計ジャッジ時間 4,039 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,760 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define MP make_pair
#define MT make_tuple
#define EACH(i,c) for(auto i: c)
#define SORT(c) sort((c).begin(),(c).end())

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()

class RollingHash{
public:
	RollingHash(const string &S)
	{
		_n = S.length();
		_pow1.resize(_n + 1);
		_pow2.resize(_n + 1);
		_hash1.resize(_n + 1);
		_hash2.resize(_n + 1);

		_pow1[0] = _pow2[0] = 1;
		_hash1[0] = _hash2[0] = 0;

		for(int i = 0; i < _n; i++){
			_pow1[i + 1] = (_pow1[i] * _b1) % _mod1;
			_pow2[i + 1] = (_pow2[i] * _b2) % _mod2;
			_hash1[i + 1] = (S[i] + _hash1[i] * _b1) % _mod1;
			_hash2[i + 1] = (S[i] + _hash2[i] * _b2) % _mod2;
		}
	}

	pair<int, int> GetHash(void){
		return make_pair(_hash1[_n], _hash2[_n]);
	}

	pair<int, int> GetHash(int l, int r){
		int h1, h2;
		h1 = ((_hash1[r] - _hash1[l] * _pow1[r - l]) % _mod1 + _mod1) % _mod1;
		h2 = ((_hash2[r] - _hash2[l] * _pow2[r - l]) % _mod2 + _mod2) % _mod2;
		return make_pair(h1, h2);
	}
private:
	int _n;
	long long _b1 = 1009, _b2 = 1007;
	long long _mod1 = 1e9 + 7, _mod2 = 1e9 + 9;
	vector<long long> _pow1, _pow2;
	vector<long long> _hash1, _hash2;
};



int main() {

	string S;
	cin >> S;
	int N = S.length();
	int M;
	cin >> M;
	VS C(M);
	REP(i, M) cin >> C[i];

	RollingHash RH(S);

	int ret = 0;
	REP(i, M){
		int m = C[i].length();
		RollingHash R(C[i]);

		REP(j, N - m + 1){
			if(R.GetHash() == RH.GetHash(j, j + m)) ret++;
		}
	}

	cout << ret << endl;

	return 0;
}
0