It's known to all that ZYB is godlike, so obviously he has a large number of titles, such as jskingjsking, bijingzybbijingzyb and nbazybnbazyb. ZYB likes his titles very much.

Each of ZYB's titles is a string consisting of lower case letters 'a'-'z''a'-'z' associated with a happiness value hihi, which shows how much ZYB likes this title. If you say any substring of some title with happiness value xx, he will get xx happiness points. Moreover, a string may appear in more than one title. In this case, the happiness points ZYB gets are multiplied. If the string you say is not the substring of any of his titles, he gets no happiness point.

For example, let's say ZYB has two titles: zybnbzybnb (with happiness value 3) and ybybybyb (with happiness value 5). If you say yy, bb or ybyb, ZYB will get 15 happiness points; if you say zz, zyzy or zybzyb, ZYB will only get 3 happiness points; if you say ybzybz or ybacybac he will get 0 happiness points.

One day, you find ZYB pretty sad. As a big fan of ZYB, you want to say a word to ZYB to cheer him up. However, ZYB is really busy, so you can only say no more than mm letters. As you haven't seen ZYB for a long time, you are so excited that you forget what you want to say, so you decide to choose to say a nonempty string no longer than mm and only containing 'a'-'z''a'-'z' with equal probability. You want to know the expectations of happiness points you will bring to ZYB for different mm.

InputThe first line contains an integer nn (1≤n≤104)(1≤n≤104), the number of titles ZYB has.

The ii-th of the next nn lines contains a nonempty string titi, which only contains lower case letters 'a'-'z''a'-'z', representing the ii-th title. The sum of lengths of all titles does not exceed 3×1053×105.

Then follows a line with nn integers hihi (1≤hi≤106)(1≤hi≤106), the happiness value of ii-th title.

The next line is a single integer QQ (1≤Q≤3×105)(1≤Q≤3×105), the number of queries.

For the next QQ lines, each contains a single integer mm (1≤m≤106)(1≤m≤106), meaning that you can say no more than mm letters to ZYB.

The input data contains only one test case.OutputFor each query, display a single line of integer, representing the answer. It can be proved that the answer can be uniquely written as p/qp/q where pp and qq are non-negative integers with gcd(p,q)=gcd(q,109+7)=1gcd(p,q)=gcd(q,109+7)=1, and you should display p⋅q−1mod(109+7)p⋅q−1mod(109+7), where q−1q−1 means the multiplicative inverse of qq modulo 109+7109+7.Sample Input

2
zybnb
ybyb
3 5
4
1
2
3
4

Sample Output

769230776
425925929
891125950
633120399

Hint

For the first query, you can bring him 3 happiness points if you say "z" or "n", and 15 happiness points if you say "y" or "b"; all other strings of length 1 bring no
happiness point to ZYB. Therefore, the expectation is (2×3+2×15)/26 = 18/13, and the answer is 18×13^(-1) mod (10^9+7) = 769230776.


题意:

给你n个字符串,然后每个字符串有一个快乐值。然后给你m个询问,每个询问给你一个长度,让你写出一个不大于这个长度的字串。这个字串的权值定义为,如果这个字符串中出现过第i个给定字符串的子串,那么权值乘以第i个字符串的快乐值,最后答案就是多个快乐值相乘。现在问你给定长度的字符串权值的期望。

思路:

由于要用到多个字符串的所有子串,所以我们很容易想到广义SAM.对于一个长度m,那么它的贡献为长度1~m所有子串的贡献和,考虑它的分母就是26,262,263...26m 的和,我们可以预处理出来。考虑它的分子就是后缀自动机上面所有出现的长度小于等于m的子串的贡献和。在后缀自动机中的parent树中,如果p所代表的子串出现的话,那么fa[p]所代表的的子串一定出现,那么len[fa[p]]+1~len[p]的长度都会出现,而且贡献为len[p]的贡献,根据right数组的定义,parent的出现次数要比x多,那么长度介于最长后缀和本身长度之间的后缀的出现次数肯定与x的出现次数相同。如果不同,那么parent肯定会指向第一个不相同的后缀对应的节点。所以说这从parent的长度加一到x的长度,这一整段的贡献我们都要计算上去。我们用一个前缀和数组sum,记录对应长度的贡献。对应的,区间 [len[fa[x]]+1,len[x]] 上的贡献都是y,表现在sum上面就是两个端点一加一减。统计完毕后,对sum求一遍前缀和,之后sum[i]表示所有长度为i的串的贡献。然后再次对sum求一次前缀和,这样的话sum[i]就表示所有长度为1~i的串的贡献。

 

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long ll;
const int maxn=1e6+;
int pw[maxn],n,m;
int flag[maxn],h[maxn],sum[maxn],ans[maxn];
string s[maxn];
bool vis[maxn];
int qpow(int a,int b)
{
int ans=;
while(b)
{
if(b&)ans=(ll)ans*a%mod;
a=(ll)a*a%mod; b>>=;
}
return ans;
}
void Init()
{
pw[]=;
for(int i=;i<maxn;i++)
pw[i]=(ll)pw[i-]*26ll%mod;
for(int i=;i<maxn;i++)
pw[i]=(pw[i]+pw[i-])%mod;
}
struct SAM{
int fa[maxn<<],l[maxn<<],nxt[maxn<<][];
int last,tot;
void Init()
{
last=tot=;
memset(nxt[tot],,sizeof nxt[tot]);
l[tot]=fa[tot]=;
}
int NewNode()
{
++tot;
memset(nxt[tot],,sizeof nxt[tot]);
l[tot]=fa[tot]=;
return tot;
}
void Insert(int ch)
{
int p,q,np,nq;
if(nxt[last][ch])
{
p=last;q=nxt[p][ch];
if(l[q]==l[p]+) last=q;//////
else
{
nq=NewNode();
l[nq]=l[p]+;fa[nq]=fa[q];
memcpy(nxt[nq],nxt[q],sizeof(nxt[q]));
fa[q]=nq;
while(p&&nxt[p][ch]==q) nxt[p][ch]=nq,p=fa[p];
last=nq;//////
}
}
else
{
np=NewNode(),p=last;
last=np; l[np]=l[p]+;
while(p&&!nxt[p][ch]) nxt[p][ch]=np,p=fa[p];
if(!p) fa[np]=;
else
{
q=nxt[p][ch];
if(l[q]==l[p]+) fa[np]=q;
else
{
nq=NewNode();
memcpy(nxt[nq],nxt[q],sizeof nxt[q]);
fa[nq]=fa[q];
l[nq]=l[p]+;
fa[q]=fa[np]=nq;
while(p&&nxt[p][ch]==q) nxt[p][ch]=nq,p=fa[p];
}
}
}
}
void cal(string s,int val,int tag)
{
int cur=;
for(int i=;s[i];i++)
{
cur=nxt[cur][s[i]-'a'];
for(int tmp=cur;tmp&&flag[tmp]!=tag;tmp=fa[tmp])
ans[tmp]=1LL*ans[tmp]*val%mod,flag[tmp]=tag;
}
}
void build(int cur)
{
vis[cur]=;
sum[l[fa[cur]]+]=(sum[l[fa[cur]]+]+ans[cur])%mod;
sum[l[cur]+]=(sum[l[cur]+]-ans[cur]+mod)%mod;
for(int i=;i<;i++)
{
int Nxt=nxt[cur][i];
if(Nxt&&!vis[Nxt]) build(Nxt);
}
}
} sam;
int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
Init(); sam.Init();
cin>>n;
for(int i=;i<=n;++i)
{
cin>>s[i]; sam.last=;
for(int j=;s[i][j];++j)
sam.Insert(s[i][j]-'a');
}
fill(ans,ans+maxn,);
for(int i=;i<=n;++i) cin>>h[i];
for(int i=;i<=n;++i) sam.cal(s[i],h[i],i);
sam.build(); sum[]=;
for(int i=;i<maxn;++i) sum[i]=(sum[i]+sum[i-])%mod;
for(int i=;i<maxn;++i) sum[i]=(sum[i]+sum[i-])%mod;
cin>>m;
while(m--)
{
int k; cin>>k;
cout<<1LL*sum[k]*qpow(pw[k],mod-)%mod<<endl;
}
return ;
}
												

HDU 6405 Make ZYB Happy(广义SAM)的更多相关文章

  1. 【HDU 4436】 str2int (广义SAM)

    str2int Problem Description In this problem, you are given several strings that contain only digits ...

  2. hdu6405Make ZYB Happy 广义sam

    题意:给出n(n<=10000)个字符串S[1~n],每个S[i]有权值val[i],随机等概率造一个由小写字母构成的字符串T,Sum = 所有含有子串T的S[i]的val[i]之积,求Sum的 ...

  3. 【BZOJ 3926】 [Zjoi2015]诸神眷顾的幻想乡 (广义SAM)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 974  Solved: 573 Descriptio ...

  4. 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)

    3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...

  5. luogu3346 诸神眷顾的幻想乡 (广义SAM)

    首先,让每一个叶节点做一次树根的话,每个路径一定至少有一次会变成直上直下的 于是对于每个叶节点作为根产生的20个trie树,把它们建到同一个广义SAM里 建法是对每个trie dfs去建,last就是 ...

  6. loj#6031. 「雅礼集训 2017 Day1」字符串(SAM 广义SAM 数据分治)

    题意 链接 Sol \(10^5\)次询问每次询问\(10^5\)个区间..这种题第一感觉就是根号/数据分治的模型. \(K\)是个定值这个很关键. 考虑\(K\)比较小的情况,可以直接暴力建SAM, ...

  7. Luogu P3181 [HAOI2016]找相同字符 广义$SAM$

    题目链接 \(Click\) \(Here\) 设一个串\(s\)在\(A\)中出现\(cnt[s][1]\)次,在\(B\)中出现\(cnt[s][2]\)次,我们要求的就是: \[\sum cnt ...

  8. CF666E Forensic Examination 广义SAM、线段树合并、倍增、扫描线

    传送门 朴素想法:对\(M\)个匹配串\(T_1,...,T_M\)建立广义SAM,对于每一次询问,找到这个SAM上\(S[pl...pr]\)对应的状态,然后计算出对于每一个\(i \in [l,r ...

  9. Luogu4022 CTSC2012 熟悉的文章 广义SAM、二分答案、单调队列

    传送门 先将所有模板串扔进广义SAM.发现作文的\(L0\)具有单调性,即\(L0\)更小不会影响答案,所以二分答案. 假设当前二分的值为\(mid\),将当前的作文放到广义SAM上匹配. 设对于第\ ...

随机推荐

  1. js数组方法大全(上)

    # js数组方法大全(上) 记录一下整理的js数组方法,免得每次要找方法都找不到.图片有点多,注意流量,嘻嘻! 本期分享 join() reverse() sort() concat() slice( ...

  2. C++中对C的扩展学习新增语法——函数重载

    函数重载 1.函数重载语法 1.同一个作用域(全局作用域.命名空间作用域.类作用域) 2.参数个数不同 3.参数类型不同 4.参数顺序不同 代码实现: 当函数名字一样的时候,通过参数类型.参数个数.参 ...

  3. PHP 当Swoole 遇上 ThinkPHP5

    本文假设你已经有了 Linux 操作系统的 PHP 环境,强烈推荐使用 Vagrant 来搭建开发环境 安装 Swoole PECL 拓展可以通过 pecl 命令或者通过源码包编译安装,本文采用 pe ...

  4. 破解微擎安装,免费搭建微擎,免费破解微擎,微擎破解版本,最新版本V2.1.2,一键安装!!

    ​ 微擎是一款基于WEB2.0(PHP+Mysql)技术架构,免费开源的公众平台管理系统,一款致力于将小程序和公众号商业化.智慧化.场景化的自助引擎.微擎提供公众号.微信小程序.支付宝小程序.百度熊掌 ...

  5. 利用Xshell配置ssh免密码登录虚拟机,进行虚拟机与物理机的传输

    先说一下 Xshell如何无密连接虚拟机: ssh登录提供两种认证方式:口令(密码)认证方式和密钥认证方式.其中口令(密码)认证方式是我们最常用的一种,这里介绍密钥认证方式登录到linux的方法.使用 ...

  6. 通过 position:fixed 实现底部导航

    通过 position:fixed 实现底部导航 HTML <div id="footer">页脚</div> CSS #footer { clear: b ...

  7. nyoj 813-对决 (i*j == k)

    813-对决 内存限制:64MB 时间限制:1000ms 特判: No 通过数:11 提交数:23 难度:0 题目描述: Topcoder 招进来了 n 个新同学,Yougth计划把这个n个同学分成两 ...

  8. poj 1001 求高精度幂(Java, BigDecimal, pow, hasNext, stripTrailingZeros, toPlainString)

    求高精度幂 Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 180325   Accepted: 43460 Descripti ...

  9. VMware NAT模式ping通外网[CentOS7]

    使用一张网卡,NAT模式 在编辑里打开虚拟网络编辑器 dhcp设置的范围 你的虚拟机的IP 就在那个范围里 NAT设置里有填网关 这里我们vmware 的设置就OK了 在去把你的网络适配器改下iP 这 ...

  10. Python常见字符串方法函数

    1.大小写转换 S.lower() S.upper() 前者将S字符串中所有大写字母转为小写,后者相反 S.title() S.capitalize() 前者返回S字符串中所有单词首字母大写且其他字母 ...