結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
AC2K
|
| 提出日時 | 2023-01-13 22:13:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,317 bytes |
| コンパイル時間 | 2,014 ms |
| コンパイル使用メモリ | 207,700 KB |
| 最終ジャッジ日時 | 2025-02-10 02:40:09 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 2 TLE * 12 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
//cout << fixed << setprecision(10);
#define rep(i, N) for(int i=0;i<(N);i++)
#define all(x) (x).begin(),(x).end()
#define popcount(x) __builtin_popcount(x)
using ll = long long;
using ld = long double;
using graph = vector<vector<int>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll infl = 1e18;
const ld eps = ld(0.000000001);
const long double pi = acos(-1);
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const int dx[4] = { 1,0,-1,0 };
const int dy[4] = { 0,1,0,-1 };
template<class T>void chmax(T&x,T y){if(x<y)x=y;}
template<class T>void chmin(T&x,T y){if(x>y)x=y;}
class RollingHash {
static const ll mod = 1e9 + 7;
const ll base;
vector<ll> powers; //base^i
static inline ll generate_base() {
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<ll> rand((ll)1, (ll)mod - 1);
return rand(engine);
}
//idの振り方
ll mapping(char c) {
return (c - 'a');
}
void expand(int siz) {
if (powers.size() < siz + 1) {
int pre_siz = powers.size();
powers.resize(siz + 1);
for (int i = pre_siz; i <= siz; i++) {
powers[i] = (powers[i - 1] * base) % mod;
}
}
}
public:
RollingHash(ll base = generate_base()) :base(generate_base()), powers{ 1 } { }
vector<ll> build(string& s) {
vector<ll> hash(s.size() + 1);
for (int i = 1; i <= s.size(); i++) {
hash[i] = (hash[i-1] * base % mod + mapping(s[i-1])) % mod;
}
return hash;
}
ll range(vector<ll>&hash,int l, int r) {
expand(r - l);
return ((hash[r] + mod - hash[l] * powers[r - l]) % mod + mod) % mod;
}
};
int main() {
string s;
cin>>s;
int m;
cin>>m;
vector<ll> c_hash(m);
RollingHash rh;
rep(i,m){
string c;
cin>>c;
auto tmp=rh.build(c);
c_hash[i]=rh.range(tmp,0,c.size());
}
int len=s.size();
auto res=rh.build(s);
unordered_map<ll,ll> cnt;
for(int i=0;i<len;i++)for(int j=i+1;j<=len;j++){
ll hash=rh.range(res,i,j);
cnt[hash]++;
}
int ans=0;
rep(i,m){
ans+=cnt[c_hash[i]];
}
cout<<ans<<endl;
}
AC2K