結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  tonegawa | 
| 提出日時 | 2020-12-01 08:23:51 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                TLE
                                 
                            (最新) 
                                AC
                                 
                            (最初) | 
| 実行時間 | - | 
| コード長 | 2,862 bytes | 
| コンパイル時間 | 1,006 ms | 
| コンパイル使用メモリ | 119,076 KB | 
| 最終ジャッジ日時 | 2025-01-16 11:06:38 | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 13 TLE * 1 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:99:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   99 |   int q;scanf("%d", &q);
      |         ~~~~~^~~~~~~~~~
            
            ソースコード
#include <iostream>
#include <vector>
#include <random>
#include <string>
#include <cassert>
#include <array>
#include <map>
#include <functional>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
struct hash64{
private:
  const static ull BASE, MOD;
  vector<ull> INV;
  inline ull MUL(ull a, ull b){
    __int128_t t = __int128_t(a) * __int128_t(b);
    t = (t >> 61) + (t & MOD);
    return (t>=MOD?t-MOD:t);
  }
  inline ull ADD(ull a, ull b){
    return (a+b>=MOD?a+b-MOD:a+b);
  }
  inline ull SUB(ull a, ull b){
    return (a<b?(MOD-b)+a:a-b);
  }
public:
  hash64(int maxlen=3000000):INV(maxlen){
    ull ret = 1, num = BASE, b = MOD-2;
    INV[0] = 1;
    while(b>0){
      if(b&1) ret = MUL(ret, num);
      num = MUL(num, num);
      b /= 2;
    }
    for(int i=1;i<maxlen;i++) INV[i] = MUL(INV[i-1], ret);
  }
  //h[i]:=hash[0, i]  O(|S|)
  inline vector<ull> hashtable(string s){
    int n = s.size();
    vector<ull> ret(n);
    ull tmp = 0, x = 1;
    for(int i=0;i<n;i++){
      tmp = ADD(tmp, MUL(x, s[i]));
      x = MUL(x, BASE);
      ret[i] = tmp;
    }
    return ret;
  }
  //hash[l, r)  O(1)
  inline ull hash(const vector<ull> &v, int l, int r){
    if(l==r) return 0;
    return MUL(SUB(v[r-1], (l==0?0:v[l-1])), INV[l]);
  }
  //tの[l, r)の中にsの[a, b)(固定長)が含まれるか O(max(|r-l| - |b-a|, 0))
  inline bool contain(const vector<ull> &s, int a, int b, const vector<ull> &t, int l, int r){
    ull pat = hash(s, a, b);
    int len = b - a;
    for(int i=l;i<=r-len;i++){
      if(MUL(SUB(t[i+len-1], (i==0?0:t[i-1])), INV[i])==pat) {
        return true;
      }
    }
    return false;
  }
  //tの[l, r)の中にsの[a, b)(固定長)が含まれるか(始点のidxを返す) O(max(|r-l| - |b-a|, 0))
  inline vector<int> search(const vector<ull> &s, int a, int b, const vector<ull> &t, int l, int r){
    ull pat = hash(s, a, b);
    int len = b - a;
    vector<int> ret;
    for(int i=l;i<=r-len;i++){
      if(MUL(SUB(t[i+len-1], (i==0?0:t[i-1])), INV[i])==pat) {
        ret.push_back(i);
      }
    }
    return ret;
  }
  //lcp(s[a, b), t[l, r))  O(log(len)), len := min(b-a, r-l)
  inline int lcp(const vector<ull> &s, int a, int b, const vector<ull> &t, int l, int r){
    int L = 0, R = min(b-a, r-l) + 1;
    while(R-L>1){
      int mid = (L+R)/2;
      if(hash(s, a, a+mid)==hash(t, l, l+mid)) L = mid;
      else R = mid;
    }
    return L;
  }
};
std::random_device seed_gen;
std::mt19937_64 engine(seed_gen());
const ull hash64::MOD((1ULL<<61)-1);
const ull hash64::BASE(engine()%hash64::MOD);
int main(){
  string s;std::cin >> s;
  hash64 h(100000);
  auto a = h.hashtable(s);
  int q;scanf("%d", &q);
  int ans = 0;
  for(int i=0;i<q;i++){
    string t;std::cin >> t;
    ans += h.search(h.hashtable(t), 0, t.size(), a, 0, s.size()).size();
  }
  printf("%d\n", ans);
}
            
            
            
        