結果

問題 No.430 文字列検索
ユーザー 白狐白狐
提出日時 2019-12-04 03:46:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 5,994 bytes
コンパイル時間 1,560 ms
コンパイル使用メモリ 125,860 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-19 05:41:18
合計ジャッジ時間 3,022 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 31 ms
4,380 KB
testcase_02 AC 51 ms
4,380 KB
testcase_03 AC 49 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 29 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 36 ms
4,380 KB
testcase_12 AC 35 ms
4,380 KB
testcase_13 AC 35 ms
4,380 KB
testcase_14 AC 35 ms
4,376 KB
testcase_15 AC 35 ms
4,380 KB
testcase_16 AC 38 ms
4,376 KB
testcase_17 AC 42 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
     ∧_∧ やあ
   (´・ω・`)     /     ようこそ、バーボンハウスへ。
   /∇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;
}
0