結果

問題 No.430 文字列検索
ユーザー Airy_PhysicsAiry_Physics
提出日時 2020-03-18 14:28:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,603 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 95,744 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-08 02:18:07
合計ジャッジ時間 6,193 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 423 ms
5,376 KB
testcase_03 AC 422 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 422 ms
5,376 KB
testcase_12 AC 420 ms
5,376 KB
testcase_13 AC 421 ms
5,376 KB
testcase_14 AC 420 ms
5,376 KB
testcase_15 AC 424 ms
5,376 KB
testcase_16 AC 424 ms
5,376 KB
testcase_17 AC 422 ms
5,376 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, 13);
    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