結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-12 07:45:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,246 bytes |
| コンパイル時間 | 1,493 ms |
| コンパイル使用メモリ | 143,560 KB |
| 最終ジャッジ日時 | 2025-01-07 17:36:55 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 4 TLE * 10 |
ソースコード
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#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>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>
using namespace std;
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}
using LL = long long;
//------------------------------------------
//------------------------------------------
struct RollingHash {
using long_type = unsigned;
using hash_type = tuple<long_type, long_type, long_type>;
private:
int s_len;
long_type mod1;
long_type mod2;
long_type mod3;
long_type base1;
long_type base2;
long_type base3;
vector<long_type> hash1;
vector<long_type> hash2;
vector<long_type> hash3;
vector<long_type> pow1;
vector<long_type> pow2;
vector<long_type> pow3;
public:
RollingHash() :
base1(1009), base2(1007), base3(2339),
mod1(1000000007), mod2(1000000009), mod3(1000000087) {
}
RollingHash(const string s) :
RollingHash() {
init(s);
}
void init(const string s) {
const int n = s.size();
s_len = n;
hash1.assign(n + 1, 0);
hash2.assign(n + 1, 0);
hash3.assign(n + 1, 0);
pow1.assign(n + 1, 1);
pow2.assign(n + 1, 1);
pow3.assign(n + 1, 1);
for (int i = 0; i < n; i++) {
hash1[i + 1] = ((hash1[i] + s[i]) * base1);
hash2[i + 1] = ((hash2[i] + s[i]) * base2);
hash3[i + 1] = ((hash3[i] + s[i]) * base3);
pow1[i + 1] = (pow1[i] * base1);
pow2[i + 1] = (pow2[i] * base2);
pow3[i + 1] = (pow3[i] * base3);
}
}
//0-index
//s[l, r)のハッシュタプルを返す
hash_type get_hash_tuple(int l, int r) {
long_type h1 = ((hash1[r] - hash1[l] * pow1[r - l] + mod1) % mod1 + mod1) % mod1;
long_type h2 = ((hash2[r] - hash2[l] * pow2[r - l] + mod2) % mod2 + mod2) % mod2;
long_type h3 = ((hash3[r] - hash3[l] * pow3[r - l] + mod3) % mod3 + mod3) % mod3;
return make_tuple(h1, h2, h3);
}
//not verify
//htype_1のハッシュの後ろに長さh2_lenのハッシュを結合する
hash_type concat(hash_type h_tup1, hash_type h_tup2, int h2_len) {
long_type h1 = (get<0>(h_tup1) * pow1[h2_len] + get<0>(h_tup2)) % mod1;
long_type h2 = (get<1>(h_tup1) * pow2[h2_len] + get<1>(h_tup2)) % mod2;
long_type h3 = (get<2>(h_tup1) * pow3[h2_len] + get<2>(h_tup2)) % mod3;
return make_tuple(h1, h2, h3);
}
//先頭i文字のハッシュタプルを返す
hash_type get_from_head(int len) {
return get_hash_tuple(0, len);
}
//末尾i文字のハッシュタプルを返す
hash_type get_from_tail(int len) {
return get_hash_tuple(s_len - len, s_len);
}
int contain(string s) {
int ret = 0;
int m_len = this->s_len;
int o_len = s.size();
RollingHash o_hash(s);
for (int i = 0; i < m_len - o_len + 1; i++) {
auto h1 = get_hash_tuple(i, i + o_len);
auto h2 = o_hash.get_hash_tuple(0, o_len);
if (RollingHash::same(h1, h2)) ret++;
}
return ret;
}
static bool same(hash_type h_tup1, hash_type h_tup2) {
return h_tup1 == h_tup2;
}
};
int main() {
string S;
cin >> S;
RollingHash roll(S);
int N;
cin >> N;
LL ans = 0;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
ans += roll.contain(s);
}
cout << ans << endl;
}