yah has nnn strings <s1,⋯,sn>< s_1, \cdots ,s_n ><s1,⋯,sn>, and he generates a sequence PPP by two steps:
- P=<s1,⋯,sn>P = < s_1, \cdots ,s_n >P=<s1,⋯,sn>
- Replace each sis_isi with all prefixes of itself.
An example is:
- the nnn strings are <aab,ab>< aab,ab ><aab,ab>
- first, P=<aab,ab>P =< aab,ab >P=<aab,ab>
- then, P=<a,aa,aab,a,ab>P =< a,aa,aab,a,ab >P=<a,aa,aab,a,ab>
There are 262626 positive integers d1,⋯,d26d_1, \cdots ,d_{26}d1,⋯,d26 that denote the difficulty to identify lowercase English letter a,b,⋯,za,b, \cdots ,za,b,⋯,z for yah.
The difficulty to identify a string strstrstr is (∏i=1∣str∣dstri)(\displaystyle\prod_{i=1}^{|str|} d_{str_i}) (i=1∏∣str∣dstri) mod mmm
Now, yah wants to calculate: for each string si(i=1,⋯,n)s_i (i = 1, \cdots ,n)si(i=1,⋯,n), the number of strings in PPP that is prefix of sis_isi and more difficult than sis_isi.
Input
The first line contains two integers n,m(1≤n≤105,1≤m≤2×105)n,m(1 \le n \le 10^5,1 \le m \le 2 \times 10^5)n,m(1≤n≤105,1≤m≤2×105).
The second line contains 262626 positive d1,⋯,d26d_1, \cdots ,d_{26}d1,⋯,d26,0≤di≤1050 \le d_i \le 10^50≤di≤105.
The following nnn lines, each line contains a string, the ithi_{th}ith lines contains sis_isi, the sum of length of all strings is not great than 2×1052 \times 10^52×105.
Output
The only line contains nnn integers, the ithi_{th}ith integer denoting the number of strings in PPP that is prefix of sis_isi but more difficult to identify for yah.
Hint
P=<a,aa,aab,a,ab>P =< a,aa,aab,a,ab >P=<a,aa,aab,a,ab>, a,aa,aa,aa,aa,aa,a are prefixes of aabaabaab with greater difculty.
输出时每行末尾的多余空格,不影响答案正确性
样例输入
2 15 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 aab ab
样例输出
3 0
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt=0;
int d[30];
int trie[100100][30];
int color[10010];
int k = 1;
string s[100100];
vector <string> v;
map<string,int>mp,cs;
typedef long long ll;
void insert(string w){
int len = w.length();
int p = 0;
for(int i=0; i<len; i++){
int c = w[i] - 'a';
if(!trie[p][c]){
trie[p][c] = k;
k++;
}
p = trie[p][c];
}
color[p] = 1;
}
int search(string s){
ll len = s.length();
ll p = 0;
for(ll i=0; i<len; i++){
ll c = s[i] - 'a';
if(!trie[p][c]) return 0;
p = trie[p][c];
}
return color[p] == 1;
}
int main(){
scanf("%d %d",&n,&m);
for(int i=0;i<26;i++){
scanf("%d",&d[i]);
}
for(int i=0;i<n;i++){
cin>>s[i];
ll ssw=1;
string ss;
for(int j=0;j<s[i].size();j++){
if(j==0) ss=s[i][j];
else ss+=s[i][j];
k=1;
insert(ss);
ssw=((d[s[i][j]-'a']%m)*(ssw%m))%m;
mp[ss]=ssw;
cs[ss]++;
}
}
ll ans=0;
for(int i=0;i<n;i++){
ans=0;string ss;
for(int j=0;j<s[i].size();j++){
if(j==0) ss=s[i][j];
else ss+=s[i][j];
if(search(ss) && mp[s[i]]<mp[ss]){
ans+=cs[ss];
}
}
printf("%lld%s",ans,(i!=n-1)?" ":"");
}
}