The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest J

传送门

yah has nnn strings <s1,⋯,sn>< s_1, \cdots ,s_n ><s1​,⋯,sn​>, and he generates a sequence PPP by two steps:

  1. P=<s1,⋯,sn>P = < s_1, \cdots ,s_n >P=<s1​,⋯,sn​>
  2. Replace each sis_isi​ with all prefixes of itself.

An example is:

  1. the nnn strings are <aab,ab>< aab,ab ><aab,ab>
  2. first, P=<aab,ab>P =< aab,ab >P=<aab,ab>
  3. 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)?" ":"");		
	}
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值