結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
renjyaku_int
|
| 提出日時 | 2020-11-22 19:55:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 650 ms / 2,000 ms |
| コード長 | 3,866 bytes |
| コンパイル時間 | 2,818 ms |
| コンパイル使用メモリ | 205,472 KB |
| 最終ジャッジ日時 | 2025-01-16 04:34:41 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const double eps = 1e-7;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const long double pi = 4 * atan(1);
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) \
{ \
cout << x << ' '; \
} \
cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
if (b < a)
{
a = b;
return 1;
}
return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
class RollingHash
{
static const ll base1 = 1009;
static const ll base2 = 2009;
static const ll mod1 = 1000000007;
static const ll mod2 = 1000000009;
vector<ll> hash1, hash2, pow1, pow2;
public:
RollingHash(const string &S)
{
int n = (int)S.size();
hash1.assign(n + 1, 0);
hash2.assign(n + 1, 0);
pow1.assign(n + 1, 1);
pow2.assign(n + 1, 1);
for (int i = 0; i < n; ++i)
{
hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
pow1[i + 1] = (pow1[i] * base1) % mod1;
pow2[i + 1] = (pow2[i] * base2) % mod2;
}
}
/**
* S の [l, r) のハッシュ値を返す
* O(1)
*/
inline pair<ll, ll> get(int l, int r) const
{
ll res1 = hash1[r] - hash1[l] * pow1[r - l] % mod1;
if (res1 < 0)
res1 += mod1;
ll res2 = hash2[r] - hash2[l] * pow2[r - l] % mod2;
if (res2 < 0)
res2 += mod2;
return make_pair(res1, res2);
}
/**
* S のハッシュ値を返す
* O(1)
*/
inline pair<ll, ll> hash() const
{
return get(0, (int)hash1.size() - 1);
}
/**
* LCP (Longest Common Prefix)
* O( log N )
*/
inline int getLCP(int a, int b) const
{
int len = min((int)hash1.size() - a, (int)hash1.size() - b);
int low = 0, high = len;
while (high - low > 1)
{
int mid = (low + high) >> 1;
if (get(a, a + mid) != get(b, b + mid))
high = mid;
else
low = mid;
}
return low;
}
/**
* hash h1 と 長さ h2_len の文字列の hash h2 を結合
*/
pair<ll, ll> concat(pair<ll, ll> h1, pair<ll, ll> h2, int h2_len)
{
return make_pair((h1.first * pow1[h2_len] + h2.first) % mod1, (h1.second * pow2[h2_len] + h2.second) % mod2);
}
};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
RollingHash h(s);
map<pair<ll, ll>, ll> count;
rep(i, s.size())
{
for (int j = i + 1; j <= min(i + 10, int(s.size())); j++)
{
count[h.get(i, j)]++;
}
}
int m;
cin >> m;
ll ans = 0;
rep(i, m)
{
string c;
cin >> c;
RollingHash ch(c);
ans += count[ch.hash()];
}
cout << ans << "\n";
}
renjyaku_int