hdu6405Make ZYB Happy 广义sam

时间:2021-07-04 15:11:01

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

题解:建一个广义sam,对于每次插入的点,我们需要更新val一遍,向suffix link也就是fa数组往前跳即可,需要打个标记,对于同一个串插入时,每个点只更新一次

数组开小了wa到死= =

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("i.in","r",stdin)
#define fout freopen("i.ans","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=1000000+10,maxn=1000000+10,inf=0x3f3f3f3f; char s[N];
vi v[10000+10];
ll ans[N<<1];
struct SAM{
int last,cnt;
int ch[N<<1][26],fa[N<<1],l[N<<1],co[N];
ll val[N<<1];
void init()
{
cnt=1;
}
void ins(int x)
{
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
val[np]=1;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
if(!p)fa[np]=1;
else
{
int q=ch[p][x];
if(l[q]==l[p]+1)fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
val[nq]=1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
}
void build(int id)
{
last=1;
int len=v[id].size();
for(int i=0;i<len;i++)ins(v[id][i]);
}
void update(int id,ll vv)
{
int len=v[id].size(),now=1;
for(int i=0;i<len;i++)
{
now=ch[now][v[id][i]];
for(int j=now;j&&co[j]<id;j=fa[j])
{
co[j]=id;
val[j]=val[j]*vv%mod;
}
}
}
void cal()
{
for(int i=1;i<=cnt;i++)
{
ans[l[i]+1]=((ans[l[i]+1]-val[i])%mod+mod)%mod;
ans[l[fa[i]]+1]=(ans[l[fa[i]]+1]+val[i])%mod;
}
for(int i=1;i<N*2;i++)ans[i]=(ans[i]+ans[i-1])%mod;
for(int i=1;i<N*2;i++)ans[i]=(ans[i]+ans[i-1])%mod;
}
}sam;
ll f[maxn];
int main()
{
fin;fout;
int n;
scanf("%d",&n);
sam.init();
for(int i=1;i<=n;i++)
{
scanf("%s",s);
int len=strlen(s);
for(int j=0;j<len;j++)v[i].pb(s[j]-'a');
sam.build(i);
}
for(int i=1;i<=n;i++)
{
int x;scanf("%d",&x);
sam.update(i,x);
}
sam.cal();
f[0]=0;
for(int i=1;i<maxn;i++)f[i]=(f[i-1]*26%mod+26)%mod;
for(int i=1;i<maxn;i++)f[i]=qp(f[i],mod-2);
int q;scanf("%d",&q);
while(q--)
{
int m;scanf("%d",&m);
printf("%lld\n",ans[m]*f[m]%mod);
}
return 0;
}
/********************
2
zybnb
ybyb
3 5
4
1
2
3
4
********************/