結果

問題 No.430 文字列検索
ユーザー 👑 tute7627tute7627
提出日時 2019-11-13 15:34:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 4,303 bytes
コンパイル時間 1,955 ms
コンパイル使用メモリ 182,508 KB
実行使用メモリ 9,592 KB
最終ジャッジ日時 2023-10-21 19:42:26
合計ジャッジ時間 2,834 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 17 ms
9,592 KB
testcase_02 AC 6 ms
5,028 KB
testcase_03 AC 5 ms
5,028 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 14 ms
7,444 KB
testcase_12 AC 14 ms
8,224 KB
testcase_13 AC 15 ms
8,224 KB
testcase_14 AC 12 ms
6,932 KB
testcase_15 AC 10 ms
6,500 KB
testcase_16 AC 9 ms
6,500 KB
testcase_17 AC 8 ms
6,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = m - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>
void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}
template<long long iden, long long size>
struct Trie{
  struct Node{
    vector<ll>next,idx;
    ll num, failure, len;
    Node(ll l):num(0), failure(-1), len(l){
       next.assign(size,-1);
    }
  };
  vector<Node>data;
  Trie(){
    data.emplace_back(0);
  }
  void add(const string &s, ll id){
    ll pos=0;
    for(ll i=0;i<s.size();i++){
      ll c = s[i] - iden;
      if(data[pos].next[c]==-1){
        data[pos].next[c]=data.size();
        data.emplace_back(i+1);
      }
      data[pos].num++;
      pos = data[pos].next[c];
    }
    data[pos].num++;
    data[pos].idx.push_back(id);
  }
  void search(const string &s){
    ll pos=0;
    for(ll i=0;i<s.size();i++){
      ll c = s[i] - iden;
      if(data[pos].next[c]==-1)break;
      pos = data[pos].next[c];
    }
  }
  vector<ll>v;
  void dfs(ll pos, ll d){
    for(ll i=0;i<size;i++){
      if(data[pos].next[i]!=-1){
        dfs(data[pos].next[i], d+1);
      }
    }
  }
  void build_Ahocorasick(){
    queue<ll>que;
    que.push(0);
    data[0].failure = -1;
    while(!que.empty()){
      ll pos = que.front();
      for(ll i=0;i<size;i++){
        if(data[pos].next[i]==-1)continue;
        ll now = data[pos].failure;
        while(now!=-1&&data[now].next[i]==-1)now = data[now].failure;
        if(now==-1)data[data[pos].next[i]].failure = 0;
        else data[data[pos].next[i]].failure = data[now].next[i];
        que.push(data[pos].next[i]);
      }
      que.pop();
    }
  }
  vector<ll> aho_search(const string &s){
    ll now=0;
    vector<ll>cnt(data.size());
    for(ll i=0;i<s.size();i++){
      ll c = s[i] - iden;
      while(now!=-1&&data[now].next[c]==-1)now=data[now].failure;
      if(now==-1)now=0;
      else{
        now=data[now].next[c];
        ll pos=now;
        while(pos!=-1){
          cnt[pos]++;
          pos=data[pos].failure;
        }
      }
    }
    return cnt;
  }
};
using trie = Trie<'A',26>;
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  //cout<<fixed<<setprecision(10);
  ll res=0,buf=0;
  bool judge = true;
  string s;cin>>s;
  ll n;cin>>n;
  trie tri;
  rep(i,0,n){
    string t;cin>>t;
    tri.add(t,i);
  }
  tri.build_Ahocorasick();
  auto cnt=tri.aho_search(s);
  rep(i,0,cnt.size())res+=cnt[i]*tri.data[i].idx.size();
  cout<<res<<endl;
  return 0;
}
0