結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-03 02:09:17 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,108 bytes |
| コンパイル時間 | 883 ms |
| コンパイル使用メモリ | 94,272 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-11-10 00:08:05 |
| 合計ジャッジ時間 | 4,225 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 1 TLE * 1 -- * 12 |
ソースコード
#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;
if(h1 < 0) h1 += _mod1;
h2 = (_hash2[r] - _hash2[l] * _pow2[r - l]) % _mod2;
if(h2 < 0) h2 += _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;
}