結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
nonpro3_shojin
|
| 提出日時 | 2019-09-23 17:41:48 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,646 bytes |
| コンパイル時間 | 667 ms |
| コンパイル使用メモリ | 74,628 KB |
| 実行使用メモリ | 11,812 KB |
| 最終ジャッジ日時 | 2024-11-10 00:33:54 |
| 合計ジャッジ時間 | 3,965 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 1 TLE * 1 -- * 12 |
ソースコード
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
#define Try 3
//デフォルトで3回、異なるBaseやMODで試行すること
/*
struct Rolling_Hash {
const ll MD[Try] = {1000000007, 999999937, 943759273 };
const ll base[Try] = {109, 313, 1289};
string X;//Xの連続部分列の中にYがあるかをチェック
vector<ll> memo[Try];
int XL;
void setup(const string& a = "") {
X = a;
for (int t = 0; t < Try; t++) {
memo[t].resize(X.size() + 1);
XL = X.size();
//(0, i]のハッシュ値の計算をする 累積和的にやる
ll powbase = 1;
memo[t][1] = X[0];
for (int i = 2; i <= XL; i++) {
powbase *= base[t], powbase %= MD[t];
memo[t][i] = (memo[t][i - 1] + powbase * X[i - 1]) % MD[t];
}
}
}
bool match(const string& Y) {
int YL = Y.size();
for (int t = 0; t < Try; t++) {
ll Yh = 0;
ll powb = 1;
for (int i = 0; i < YL; i++) {
Yh = (Yh + Y[i] * powb) % MD[t];
powb *= base[t], powb %= MD[t];
}
for (int i = 0; i + YL <= XL; i++) {
ll Xh = (memo[t][i + YL] - memo[t][i] + MD[t]) % MD[t];
if (Xh == Yh)return true;
Yh = (Yh * base[t]) % MD[t];
}
}
return false;
}
};
*/
struct Rolling_Hash {
const ll MD[Try] = { 999999937, 1000000007, 943759273 };
const ll base[Try] = { 313, 109, 1289 };
string X;//Xの連続部分列の中にYがあるかをチェック
vector<ll> memo[Try];
int XL;
int f[100000] = {};
void setup(const string& a = "") {
X = a;
for (int t = 0; t < Try; t++) {
memo[t].resize(X.size() + 1);
XL = X.size();
//(0, i]のハッシュ値の計算をする 累積和的にやる
ll powbase = 1;
memo[t][1] = X[0];
for (int i = 2; i <= XL; i++) {
powbase *= base[t], powbase %= MD[t];
memo[t][i] = (memo[t][i - 1] + powbase * X[i - 1]) % MD[t];
}
}
}
int match(const string& Y) {
int ret = 0;
int YL = Y.size();
map<int, int> tmp;
for (int t = 0; t < Try; t++) {
ll Yh = 0;
ll powb = 1;
for (int i = 0; i < YL; i++) {
Yh = (Yh + Y[i] * powb) % MD[t];
powb *= base[t], powb %= MD[t];
}
for (int i = 0; i + YL <= XL; i++) {
ll Xh = (memo[t][i + YL] - memo[t][i] + MD[t]) % MD[t];
if (Xh == Yh)tmp[i]++;
Yh = (Yh * base[t]) % MD[t];
}
}
for (auto it = tmp.begin(); it != tmp.end(); it++) {
if ((*it).second == Try)ret++;
}
return ret;
}
};
int main() {
string S;
int M;
cin >> S >> M;
Rolling_Hash rh;
rh.setup(S);
int ans = 0;
for (int i = 0; i < M; i++) {
string C;
cin >> C;
ans += rh.match(C);
}
cout << ans << endl;
return 0;
}
nonpro3_shojin