結果

問題 No.430 文字列検索
ユーザー しらっ亭しらっ亭
提出日時 2016-10-02 22:52:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 3,096 bytes
コンパイル時間 1,289 ms
コンパイル使用メモリ 168,972 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 17:22:43
合計ジャッジ時間 4,635 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 251 ms
4,376 KB
testcase_02 AC 256 ms
4,376 KB
testcase_03 AC 266 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 252 ms
4,376 KB
testcase_12 AC 252 ms
4,380 KB
testcase_13 AC 264 ms
4,380 KB
testcase_14 AC 254 ms
4,376 KB
testcase_15 AC 253 ms
4,376 KB
testcase_16 AC 251 ms
4,376 KB
testcase_17 AC 256 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
#define BIT(n) (1LL<<(n))
#define SZ(x) ((int)(x).size())
#define fst first
#define snd second
using ll=long long;using pii=pair<int,int>;using vb=vector<bool>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;

struct RollingHash64 {
  const int base = 1e9+7;
  const int N;
  vector<long long> P, H;

  /**
   * O(N)
   */
  RollingHash64(const string & S) : N(S.size()), P(N+1, 1), H(N+1, 0) {
    for (int i = 0; i < N; i++) {
      int c = (int) (S[i] - ' ');
      H[i+1] = H[i] * base + c;
      P[i+1] = P[i] * base;
    }
  }

  /**
   * S の [l, r) のハッシュ値を返す
   * O(1)
   */
  long long hash(int l, int r) const {
    return H[r] - H[l] * P[r - l];
  }

  /**
   * S の [l1, r1) と [l2, r2) のハッシュ値が等しいかを返す
   * O(1)
   */
  bool equals(int l1, int r1, int l2, int r2) const {
    if (l1 < 0 || l2 < 0) return false;
    if (r1 > N || r2 > N) return false;
    if (r1 - l1 != r2 - l2) return false;
    return hash(l1, r1) == hash(l2, r2);
  }

  /**
   * S の p1 からの len 文字と p2 からの len 文字が等しいかを返す
   * O(1)
   */
  bool equals(int p1, int p2, int len) const {
    return equals(p1, p1 + len, p2, p2 + len);
  }

  /**
   * S の pos 以降と other が等しいかを返す
   * O(1)
   */
  bool equals(int pos, const RollingHash64 &other) const {
    int r = pos + other.N;
    if (r > N) return false;

    _p("(%d,%d):(%d,%d)\n", pos,r,0,other.N);
    return hash(pos, r) == other.hash(0, other.N);
  }

  /**
   * S の p1 からと p2 からで共通する文字列の長さを返す。
   * O(log(N))
   */
  int lcp(int p1, int p2) const {
    int ok = 0;
    int ng = min(N - p1, N - p2) + 1;
    while (ng - ok > 1) {
      int mid = (ok + ng) / 2;
      (equals(p1, p1 + mid, p2, p2 + mid) ? ok : ng) = mid;
    }
    return ok;
  }
};

void Main() {
  string S;
  int M;
  cin >> S >> M;
  int N = SZ(S);

  RollingHash64 rh(S);

  int ans = 0;

  rep(i, M) {
    string s;
    cin >> s;
    int l = SZ(s);
    RollingHash64 sub(s);
    ll h = sub.hash(0, sub.N);
    int x = 0;

    rep(p, N-l+1) {
      bool c = rh.hash(p, p+l) == h;
      //_p("%d:%d: %d\n", i,p,c);
      if (c) x++;
    }
    //_p("%d: %d\n", i, x);
    ans += x;
  }

  cout << ans << endl;
}
int main() { cin.tie(0); ios::sync_with_stdio(false); Main(); return 0; }
0