結果

問題 No.430 文字列検索
ユーザー yukim0359yukim0359
提出日時 2024-06-23 00:44:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,652 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 176,152 KB
実行使用メモリ 26,368 KB
最終ジャッジ日時 2024-06-23 00:44:15
合計ジャッジ時間 3,273 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 27 ms
5,376 KB
testcase_03 AC 26 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 228 ms
26,368 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 15 ms
5,888 KB
testcase_11 AC 88 ms
7,552 KB
testcase_12 AC 90 ms
7,552 KB
testcase_13 AC 95 ms
7,552 KB
testcase_14 AC 76 ms
6,016 KB
testcase_15 AC 60 ms
5,376 KB
testcase_16 AC 34 ms
5,376 KB
testcase_17 AC 29 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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 mod = 998244353;
int base = 27;

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

ll base_n[11];
map <ll, int> mp;

ll RollingHash(string pattern){
  int p_len = pattern.length();
  ll p_hash = 0;
  rep0(i, p_len){
    p_hash += base_n[p_len-1-i] * (ll)(pattern[i] + 1 - 'A');
    p_hash %= mod;
  }
  return p_hash;
}

void MakeRollingHashMap(string text){
  int t_len = text.length();
  rep1(len, min(10, t_len)){
    ll t_hash = 0;
    rep0(i, len){
      t_hash += base_n[len-1-i] * (ll)(text[i] + 1 - 'A');
      t_hash %= mod;
    }
    if(mp.count(t_hash)) mp[t_hash] += 1;
    else mp[t_hash] = 1;

    // 先頭文字でforを回してt_hashを更新していく
    for(int i=1; i<=t_len-len; ++i){
      ll new_hash = (base * t_hash) % mod;
      new_hash = (new_hash - base_n[len] * (text[i-1] + 1 - 'A') + mod) % mod;
      new_hash += (text[i + len - 1] + 1 - 'A');
      new_hash %= mod;
      t_hash = new_hash;
      if(mp.count(t_hash)) mp[t_hash] += 1;
      else mp[t_hash] = 1;
    }
  }
}


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

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

  int ans = 0;
  rep1(i, M){
    string C;
    cin >> C;
    ll hsh = RollingHash(C);
    if(mp.count(hsh)){
      ans += mp[hsh];
    }
  }
  cout << ans << endl;
}
0