結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-05-20 13:42:52 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,432 bytes |
| コンパイル時間 | 2,086 ms |
| コンパイル使用メモリ | 201,480 KB |
| 最終ジャッジ日時 | 2025-01-29 09:41:41 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 4 TLE * 10 |
ソースコード
#include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;
vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
fact.resize(n + 5);
factinv.resize(n + 5);
inv.resize(n + 5);
fact.at(0) = fact.at(1) = 1;
factinv.at(0) = factinv.at(1) = 1;
inv.at(1) = 1;
for (ll i = 2; i < n + 5; i++) {
fact.at(i) = (fact.at(i - 1) * i) % mod;
inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
}
}
ll nCk(ll n, ll k) {
if (n < k) return 0;
return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
bool chmax(ll& p, ll q) {
if (p < q) {
p = q;
return 1;
}
else {
return 0;
}
}
bool chmin(ll& p, ll q) {
if (p > q) {
p = q;
return 1;
}
else {
return 0;
}
}
ll sti(string S) {
ll res = 0;
rep(i, S.size()) {
res += S[i] - '0';
res *= 10;
}
return res / 10;
}
string ist(ll n, ll L) {
string res = "";
rep(l, L)res += '0';
ll p = L - 1;
while (n > 0) {
res[p] = char(n % 10 + '0');
p--;
n /= 10;
}
return res;
}
ll gcd(ll(a), ll(b)) {
ll c = a;
while (a % b != 0) {
c = a % b;
a = b;
b = c;
}
return b;
}
struct rollinghash{
const ll base=9973;
const vector<ll> MoD = {999999937, 1000000007};
string S;
vector<ll> hash[2], power[2];
rollinghash(const string &cs) { init(cs); }
ll Shash(string T,ll iter){
ll n = T.size();
ll reshash=0;
ll respow=1;
rep(i,n){
reshash = (reshash*base + T[i]) % MoD[iter];
}
return reshash;
}
void init(const string &cs) {
S = cs;
ll n = S.size();
rep(iter,MoD.size()) {
hash[iter].assign(n+1, 0);
power[iter].assign(n+1, 1);
rep(i,n) {
hash[iter][i+1] = (hash[iter][i] * base + S[i]) % MoD[iter];
power[iter][i+1] = power[iter][i] * base % MoD[iter];
}
}
}
ll get(int l, int r, int id = 0) const {
return ((hash[id][r] - (hash[id][l] * power[id][r-l]) % MoD[id])+MoD[id])%MoD[id];
}
ll getLCP(int a, int b) const {
int len = min((int)S.size()-a, (int)S.size()-b);
int low = -1, high = len + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (get(a, a+mid, 0) != get(b, b+mid, 0)) high = mid;
else if (get(a, a+mid, 1) != get(b, b+mid, 1)) high = mid;
else low = mid;
}
return low;
}
};
int main() {
//cin.tie(nullptr);
//ios::sync_with_stdio(false);
string S;
cin>>S;
rollinghash rh(S);
ll N=S.size();
ll M;
cin>>M;
ll an=0;
rep(m,M){
string T;
cin>>T;
ll P=T.size();
ll CH=rh.Shash(T,0);
rep(i,N){
if(i+P>N)break;
ll CB=rh.get(i,i+P);
if(CH==CB)an++;
}
}
cout<<an<<endl;
}