Language: C++


#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

const int N = (int)3e5;

ll mpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
  else return mpow(a,b-1) * a % MOD;
}
ll gcd(ll a, ll b){
  if(b==0) return a;
  else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
  if(kaijo_memo.size() > n) return kaijo_memo[n];
  if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
  while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
  return kaijo_memo[n];
}
ll nCr(ll n, ll r){
  if(n < r || r < 0) return 0;
  ll ret = 1;
  ret *= kaijo(n); ret %= MOD;
  ret *= mpow(kaijo(r), MOD-2); ret %= MOD;
  ret *= mpow(kaijo(n-r), MOD-2); ret %= MOD;
  return ret;
}

vector<vector<pair<string,int>>> G;
vector<vector<int>> size;
  //ノードの圧縮を行う
  //再帰的に, 一個下の階層のsaikiのreturnを新たに子とする
  //子が1つしかないならその子のsaikiのreturnを返し,
  //複数ある場合は自分自身を返す

pair<string,int> saiki(int now){
  if(G[now].size() == 1){
    pair<string,int> ret = saiki(G[now][i].second);
    ret.first.append(G[now][i].first);
    return ret;
  }
  rep(i, G[now].size()){
    pair<string,int> ret = saiki(G[now][i].second);
    ret.first.append(G[now][i].first);
    G[now][i] = ret;
  }
  if(G[now].size()==1){
    return pair<string,int>{G[now][0].first,G[now][0].second};
  }else if(G[now].size()==0){
    return pair<string,int>{"", now};
  }else{
    return pair<string,int>{"", now};
  }
}

int count_size(int now){
  int sum = 0;
  rep(i, G[now].size()){
    int ret = count_size(G[now][i].second);
    sum += ret;
    size[now].push_back(ret);
  }
  if(G[now].size() == 0){
    sum++;
  }
  return sum;
}

  //各ノードにおいて, 自分より若いものの個数を加えていく
  //一致するものがあればその子ノードに対してsaikiし, そのreturnも加える
  //最後にその和をreturnする
//0,0,s,p2i
int calc(int now, int si, string s, vector<int> p2i){
  /*rep(i,si){
    cerr<<" ";
  }
  cerr<<"now:"<<now<<"s:"<<s<<"si:"<<si<<endl;*/
  int sum = 0;
  rep(i, G[now].size()){
    if(p2i[G[now][i].first.back() - '`'] > p2i[s[si] - '`']){
      sum += size[now][i];
    }else if(p2i[G[now][i].first.back() - '`'] == p2i[s[si] - '`']){
      sum += calc(G[now][i].second, si + G[now][i].first.size(), s, p2i);
    }
  }
  /*rep(i,si){
    cerr<<" ";
  }
  cerr<<sum<<endl;*/
  return sum;
}

int main(void){
  int n,q;cin>>n;
  vector<string> S;
  rep(i,n){
    string s;cin>>s;
    S.push_back(s + "`");
  }
  cin>>q;
  vector<pair<int,string>> query;
  rep(i,q){
    int k; string p; cin>>k>>p;
    query.push_back({k,p});
  }

  G.push_back(vector<pair<string,int>>());
  //グラフを生成する 番号を割り振っていく
  //各ノードは辺(string,to)のリストを持つ
  //この段階では, stringの文字数は必ず1
  //該当するstringがあればそこに遷移し, なければ子を新しく生成
  rep(i,n){
    string s = S[i];
    int now = 0;
    rep(si, s.size()){
      int next = -1;
      rep(gi, G[now].size()){
        if(s[si] == G[now][gi].first[0]){
          next = G[now][gi].second;
          break;
        }
      }
      if(next == -1){
        next = G.size();
        G[now].push_back({s.substr(si,1), next});
        G.push_back(vector<pair<string,int>>());
        now = next;
      }else{
        now = next;
      }
    }
  }
  /*rep(i, G.size()){
    rep(j, G[i].size()){
      cerr<<i<<" "<<G[i][j].first<<" "<<G[i][j].second<<endl;
    }
  }*/

  //ノードの圧縮を行う
  //再帰的に, 一個下の階層のsaikiのreturnを新たに子とする
  //子が1つしかないならその子のsaikiのreturnを返し,
  //複数ある場合は自分自身を返す
  saiki(0);
  return 0;
  /*cerr<<endl;
  rep(i, G.size()){
    rep(j, G[i].size()){
      cerr<<i<<" "<<G[i][j].first<<" "<<G[i][j].second<<endl;
    }
  }*/
  //return 0;
  //葉の個数をカウント
  rep(i,G.size()){
    size.push_back(vector<int>());
  }
  count_size(0);
  //return 0;
  /*cerr<<endl;
  rep(i, G.size()){
    rep(j, G[i].size()){
      cerr<<i<<" "<<G[i][j].first<<" "<<G[i][j].second<<"size:"<<size[i][j]<<endl;
    }
  }*/

 //queryを処理する
  //各ノードにおいて, 自分より若いものの個数を加えていく
  //一致するものがあればその子ノードに対してsaikiし, そのreturnも加える
  //最後にその和をreturnする
  rep(i,q){
    int k = query[i].first;
    string p = query[i].second;
    string s = S[k-1];
    vector<int> p2i;
    rep(i,27){
      p2i.push_back(0);
    }
    rep(i,26){
      p2i[p[i]-'`'] = i+1;
    }
    cout<<n - calc(0,0,s,p2i)<<endl;
  }
  

  return 0;
}


Compiler output:
a.cc: In function 'std::pair<std::__cxx11::basic_string<char>, int> saiki(int)':
a.cc:65:41: error: 'i' was not declared in this scope
   65 |     pair<string,int> ret = saiki(G[now][i].second);
      |                                         ^
a.cc: In function 'int count_size(int)':
a.cc:88:5: error: reference to 'size' is ambiguous
   88 |     size[now].push_back(ret);
      |     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:57:21: note:                 'std::vector<std::vector<int> > size'
   57 | vector<vector<int>> size;
      |                     ^~~~
a.cc: In function 'int calc(int, int, std::string, std::vector<int>)':
a.cc:108:14: error: reference to 'size' is ambiguous
  108 |       sum += size[now][i];
      |              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:57:21: note:                 'std::vector<std::vector<int> > size'
   57 | vector<vector<int>> size;
      |                     ^~~~
a.cc: In function 'int main()':
a.cc:181:5: error: reference to 'size' is ambiguous
  181 |     size.push_back(vector<int>());
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:57:21: note:                 'std::vector<std::vector<int> > size'
   57 | vector<vector<int>> size;
      |                     ^~~~