結果

問題 No.430 文字列検索
ユーザー matsuura yutomatsuura yuto
提出日時 2023-07-24 23:23:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,617 ms / 2,000 ms
コード長 1,767 bytes
コンパイル時間 1,852 ms
コンパイル使用メモリ 168,596 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 18:42:38
合計ジャッジ時間 16,106 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1,617 ms
6,944 KB
testcase_02 AC 1,261 ms
6,944 KB
testcase_03 AC 1,261 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 10 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 1,549 ms
6,940 KB
testcase_12 AC 1,250 ms
6,940 KB
testcase_13 AC 1,257 ms
6,944 KB
testcase_14 AC 1,267 ms
6,944 KB
testcase_15 AC 1,257 ms
6,940 KB
testcase_16 AC 1,256 ms
6,944 KB
testcase_17 AC 1,259 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
class rolling_hash {
public:
	using u64 = std::uint_fast64_t;
	using size_type = std::uint_fast32_t;
	using i128 = __int128_t;
 
	static constexpr u64 MOD = (1uL << 61) - 1;
	static constexpr u64 base = 20200213;
 
	std::string str;
	std::vector<u64> hash_, pow;
 
private:
	static constexpr u64 mask30 = (1ul << 30) - 1;
	static constexpr u64 mask31 = (1ul << 31) - 1;
 
	u64 mul(i128 a, i128 b) const {
		i128 t = a * b;
		t = (t >> 61) + (t & MOD);
		
		if(t >= MOD) return t - MOD;
		return t;
	}
	size_type xorshift(size_type x) const {
		x ^= x << 13;
		x ^= x >> 17;
		x ^= x << 5;
		return x;
	}
 
public:
	rolling_hash(const rolling_hash &) = default;
	rolling_hash(rolling_hash&&) = default;
 
	rolling_hash() : str() {};
	rolling_hash(const std::string & str) : str(str) {
		hash_.resize(str.size() + 1, 0);
		pow.resize(str.size() + 1, 1);
		for(size_type i = 0; i < str.size(); i++) {
			hash_[i + 1] = mul(hash_[i], base) + xorshift(str[i] + 1);
			pow[i + 1] = mul(pow[i], base);
			if(hash_[i + 1] >= MOD) hash_[i + 1] -= MOD;
		}
	}
 
	u64 hash() const { return hash_.back(); }
	u64 hash(size_type l, size_type r) const {
		u64 ret = MOD + hash_[r] - mul(hash_[l], pow[r - l]);
		return ret < MOD ? ret : ret - MOD;
	}
	
	size_type size() const { return str.size(); }
};
int main() {
  string S; cin>>S;
  int N; cin>>N;
  string T[N];
  for(int i=0;i<N;i++)cin>>T[i];
  long long ans=0;
  rolling_hash hash1(S);
  for(int i=0;i<N;i++){
    if(S.size()<T[i].size())continue;
    rolling_hash hash2(T[i]);
    for(int j=0;j<=S.size()-T[i].size();j++){
      if(hash1.hash(j,j+T[i].size())==hash2.hash(0,T[i].size()))ans++;
    }
  }
  cout<<ans<<endl;
}
0