結果

問題 No.430 文字列検索
ユーザー moharan627moharan627
提出日時 2022-05-11 12:30:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 3,119 bytes
コンパイル時間 5,132 ms
コンパイル使用メモリ 315,308 KB
実行使用メモリ 26,648 KB
最終ジャッジ日時 2023-09-26 01:39:05
合計ジャッジ時間 7,333 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 318 ms
26,648 KB
testcase_02 AC 9 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 305 ms
26,328 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 13 ms
5,780 KB
testcase_11 AC 61 ms
7,396 KB
testcase_12 AC 63 ms
7,868 KB
testcase_13 AC 62 ms
7,588 KB
testcase_14 AC 47 ms
6,288 KB
testcase_15 AC 36 ms
5,160 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 12 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if !__INCLUDE_LEVEL__

#include __FILE__
int main()
{
    string S;cin >> S;
    ll L = S.size();
    RH rh(S);
    map <ll,ll> hashnum;
    range(len,1,11){
        range(l,0,L-len+1){
            ll hash = rh.get(l,l+len);
            hashnum[hash]+=1;
        }
    }
    ll N;cin >> N;
    ll Ans = 0;
    
    rep(n,N){
        string s;cin >> s;
        Ans += hashnum[rh.gethash(s)];
    }
    cout << Ans << endl;
}

#else

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rrep(i, n) for(int i = n; i >= 0; i--)
#define range(i, m, n) for(int i = m; i < n; i++)
#define fore(i,a) for(auto &i:a)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sum(v) accumulate(all(v),0)
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
const ll INF = 1e16;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll SN(char s){return ll(s-'0');}
ll SN(string s){return stoll(s);}
int alpN(char s){return int(s-'a');}
int AlpN(char s){return int(s-'a');}
using Graph = vector<vector<ll>>;
using mint1 = modint1000000007;
using mint2 = modint998244353;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){for(int i=0;i<(int)v.size();i++)o<<(i>0?" ":"")<<v[i];return o;}//vector空白区切り出力
template< unsigned mod >
struct RollingHash {
  vector< unsigned > hashed, power;

  inline unsigned mul(unsigned a, unsigned b) const {
    unsigned long long x = (unsigned long long) a * b;
    unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
    asm("divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (mod));
    return m;
  }

  RollingHash(const string &s, unsigned base = 10007) {
    int sz = (int) s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);
    power[0] = 1;
    for(int i = 0; i < sz; i++) {
      power[i + 1] = mul(power[i], base);
      hashed[i + 1] = mul(hashed[i], base) + s[i];
      if(hashed[i + 1] >= mod) hashed[i + 1] -= mod;
    }
  }
  unsigned gethash(const string&s, unsigned base = 10007){
    int sz = (int) s.size();
    unsigned ret = 0;
    for(int i = 0; i < sz; i++) {
        ret = mul(ret,base)+s[i];
        if(ret >= mod)ret-=mod;
    }
    return ret;
  }


  unsigned get(int l, int r) const {
    unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
    if(ret >= mod) ret -= mod;
    return ret;
  }

  unsigned connect(unsigned h1, int h2, int h2len) const {
    unsigned ret = mul(h1, power[h2len]) + h2;
    if(ret >= mod) ret -= mod;
    return ret;
  }

  int LCP(const RollingHash< mod > &b, int l1, int r1, int l2, int r2) {
    int len = min(r1 - l1, r2 - l2);
    int low = -1, high = len + 1;
    while(high - low > 1) {
      int mid = (low + high) / 2;
      if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid;
      else high = mid;
    }
    return (low);
  }
};

using RH = RollingHash< 1000000007 >;
#endif
0