結果
| 問題 |
No.430 文字列検索
|
| ユーザー |
白狐
|
| 提出日時 | 2019-12-04 03:46:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 44 ms / 2,000 ms |
| コード長 | 5,994 bytes |
| コンパイル時間 | 1,619 ms |
| コンパイル使用メモリ | 122,916 KB |
| 最終ジャッジ日時 | 2025-01-08 07:39:20 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
/*
∧_∧ やあ
(´・ω・`) / ようこそ、バーボンハウスへ。
/∇y:::::\ [ ̄] このテキーラはサービスだから、まず飲んで落ち着いて欲しい。
|:⊃:|:::::| |──|
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| うん、「また」なんだ。済まない。
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| ̄ 仏の顔もって言うしね、謝って許してもらおうとも思っていない。
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄/|
∇ ∇ ∇ ∇ /./| でも、この提出を見たとき、君は、きっと言葉では言い表せない
┴ ┴ ┴ ┴ / / | 「ときめき」みたいなものを感じてくれたと思う。
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|/ | 殺伐としたコンテストの中で、そういう気持ちを忘れないで欲しい
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | そう思って、この提出を投げたんだ。
(⊆⊇) (⊆⊇) (⊆⊇) |
|| || || | じゃあ、判定を聞こうか。
./|\ /|\ /|\
*/
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define ALL(obj) (obj).begin(),(obj).end()
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i = (b-1);i>=a;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define SIZE(x) ((int)(x).size())
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef pair<int, int> pint;
typedef pair<lint, lint> plint;
typedef vector<lint> vec;
typedef vector<vector<lint>> matrix;
typedef priority_queue<lint> p_que;
typedef priority_queue<lint, vector<lint>, greater<lint>> p_que_rev;
const lint INF = INT_MAX;
const lint LINF = LLONG_MAX;
const lint MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};
lint gcd(lint a, lint b) {
lint r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
lint lcm(lint a, lint b) {
return (a / gcd(a, b)) * b;
}
lint power(lint x, lint n, lint mod = MOD) {
lint ret = 1;
while(n > 0) {
if(n & 1){
(ret *= x) %= mod;
}
(x *= x) %= mod;
n >>= 1;
}
return ret;
}
vector<lint> make_power(int n, lint base){
lint num = 1;
vector<lint> ret;
for (int i=0; i<=n; ++i){
ret.push_back(num);
num *= base;
}
return ret;
}
struct SuffixArray {
const string s;
vector<int> SuffixArr;
// 初期化
SuffixArray(const string &str) : s(str){
const int len = str.length();
vector<int> rank(len);
vector<int> tmp(len, 0);
int k;
SuffixArr.resize(len);
REP(i, len){
SuffixArr[i] = i;
rank[i] = s[i];
}
// ソート比較用関数 ([&] は変数の参照キャプチャ)
auto compare_sa = [&](int i, int j){
if(rank[i] != rank[j]){
return rank[i] < rank[j];
}
int ri = (i + k < len) ? rank[i + k] : -1;
int rj = (j + k < len) ? rank[j + k] : -1;
return ri < rj;
};
for (k = 1; k <= len; k *= 2){
sort(SuffixArr.begin(), SuffixArr.end(), compare_sa);
tmp[SuffixArr[0]] = 0;
for (int i = 1; i < len; ++i){
int c = compare_sa(SuffixArr[i - 1], SuffixArr[i]) ? 1 : 0;
tmp[SuffixArr[i]] = tmp[SuffixArr[i - 1]] + c;
}
REP(i, len){
rank[i] = tmp[i];
}
}
}
// S の s_idx 文字目からの文字列と, P の p_idx 文字目からの文字列の比較
bool substr_less_than(const string &p, int s_idx = 0, int p_idx = 0){
int len_s = s.length();
int len_p = p.length();
while(s_idx < len_s && p_idx < len_p){
if(s[s_idx] < p[p_idx]){
return true;
}
else if(s[s_idx] > p[p_idx]){
return false;
}
s_idx++;
p_idx++;
}
return (s_idx >= len_s) && (p_idx < len_p);
}
pair<int, int> lower_upper_bound(string &p){
int lb, ub;
auto binary_search = [&](int lo, int hi){
while(hi - lo > 1){
int mid = (hi + lo) / 2;
if(substr_less_than(p, SuffixArr[mid])){
lo = mid;
}
else {
hi = mid;
}
}
return hi;
};
// lower_bound
lb = binary_search(-1, s.length());
// upper_bound
p.back()++;
ub = binary_search(lb - 1, s.length());
return make_pair(lb, ub);
}
};
// validated from ALDS1_14_D
string s;
int m;
vector<string> c;
void input(){
cin >> s;
cin >> m;
c.resize(m);
REP(i, m){
cin >> c[i];
}
return;
}
void solve(){
lint ans = 0;
SuffixArray SA(s);
REP(i, m){
auto p = SA.lower_upper_bound(c[i]);
ans += p.snd - p.fst;
}
cout << ans << endl;
return;
}
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
input();
solve();
return 0;
}
白狐