結果

問題 No.430 文字列検索
ユーザー Airy_PhysicsAiry_Physics
提出日時 2020-03-18 14:34:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 1,604 bytes
コンパイル時間 688 ms
コンパイル使用メモリ 94,276 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-20 19:54:34
合計ジャッジ時間 5,074 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <complex>

using ll = long long int;
using namespace std;

long long int count_contain(std::string a, std::string b, unsigned long long hash_base);


int main(){

  string S, strtmp;
  ll M;
  vector<string> C;

  cin >> S;
  cin >> M;
  for(ll i = 0; i < M; i++){
    cin >> strtmp;
    C.push_back(strtmp);
  }

  ll ans = 0;
  for(ll i = 0; i < M; i++){
    ans += count_contain(C[i], S, 113);
    cerr << ans << endl;
  }

  cerr << "Answer:" << endl;
  cout << ans << endl;

  return 0;

}

// if b[i,i+a_len) = a then return i (i >= 0)
// else return -1
long long int count_contain(std::string a, std::string b, unsigned long long hash_base){
  // get length of a and b
  long long int a_len = a.length();
  long long int b_len = b.length();

  // if a is longer than b
  if(a_len > b_len){
    return 0;
  }

  // calculate hash_a
  unsigned long long hash_a = 0;
  for(long long int i = 0; i < a_len; i++){
    hash_a = hash_a*hash_base + a[i];
  }

  // solve problem
  unsigned long long hash_b = 0;
  unsigned long long tmp = 1;

  long long int ans = 0;

  for(long long int i = 0; i < a_len; i++){
    hash_b = hash_b*hash_base + b[i];
    tmp *= hash_base;
  }
  if(hash_a == hash_b){
    ans++;
  }

  for(long long int i = 1; i <= b_len - a_len; i++){
    hash_b = hash_b*hash_base + b[i+a_len-1] - tmp*b[i-1];
    if(hash_b == hash_a){
      ans++;
    }
  }

  return ans;
}

0