結果

問題 No.430 文字列検索
ユーザー yukim0359yukim0359
提出日時 2024-06-22 20:49:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,526 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 170,116 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-06-22 20:49:46
合計ジャッジ時間 5,041 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

//  https://yukicoder.me/problems/no/430

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep0(i,n) for(int i=0; i<n; ++i)
#define rep1(i,n) for(int i=1; i<=n; ++i)

ll powpow(ll x, ll n){
  if(n==0) return 1;
  ll tmp = powpow(x, n/2);
  if(n % 2 == 1) return (tmp * tmp * x);
  else return (tmp * tmp);
}

ll mod = 998244353;
int base = 26;

ll powpowm(ll x, ll n){
  if(n==0) return 1;
  ll tmp = powpow(x, n/2);
  if(n % 2 == 1) return (((tmp * tmp) % mod) * x) % mod;
  else return (tmp * tmp) % mod;
}

ll base_n[11];

int RollingHashMatch(string text, string pattern){
  int ans = 0;
  int t_len = text.length();
  int p_len = pattern.length();
  ll t_hash = 0;
  ll p_hash = 0;
  rep0(i, p_len){
    p_hash += base_n[p_len-1-i] * (ll)(pattern[i]-'A');
    p_hash %= mod;
    t_hash += base_n[p_len-1-i] * (ll)(text[i]-'A');
    t_hash %= mod;
  }
  if(p_hash == t_hash) ans += 1;

  // 先頭文字でforを回してt_hashを更新していく
  rep1(i, t_len - p_len){
    ll new_hash = base * t_hash;
    new_hash -= base_n[p_len] * (text[i-1]-'A');
    new_hash += (text[i + p_len - 1] - 'A');
    new_hash %= mod;
    if(new_hash < 0) new_hash += mod;
    t_hash = new_hash;
    if(p_hash == t_hash) ans += 1;
  }
  return ans;
}

int main(){
  string S;
  cin >> S;
  int M;
  cin >> M;

  rep0(i, 11) base_n[i] = powpowm(base, i);

  int ans = 0;
  rep1(i, M){
    string C;
    cin >> C;
    ans += RollingHashMatch(S, C);
  }
  cout << ans << endl;
}
0