您的位置:首页 > 其它

hdu 4436 后缀自动机算和

2017-10-20 20:00 330 查看
In this problem, you are given several strings that contain only digits from '0' to '9', inclusive. 

An example is shown below. 

101 

123 

The set S of strings is consists of the N strings given in the input file, and all the possible substrings of each one of them. 

It's boring to manipulate strings, so you decide to convert strings in S into integers. 

You can convert a string that contains only digits into a decimal integer, for example, you can convert "101" into 101, "01" into 1, et al. 

If an integer occurs multiple times, you only keep one of them. 

For example, in the example shown above, all the integers are 1, 10, 101, 2, 3, 12, 23, 123. 

Your task is to calculate the remainder of the sum of all the integers you get divided by 2012. 

InputThere are no more than 20 test cases. 

The test case starts by a line contains an positive integer N. 

Next N lines each contains a string consists of one or more digits. 

It's guaranteed that 1≤N≤10000 and the sum of the length of all the strings ≤100000. 

The input is terminated by EOF. 

OutputAn integer between 0 and 2011, inclusive, for each test case.
Sample Input
5
101
123
09
000
1234567890


Sample Output
202

题意:让求所有不同的字符串的和
由于后缀自动机所有的状态加起来就是不同的子串的总数目。如果算重复的话每个状态还要乘上每个集合的sz。由于是不算重复的。所有找出每种前缀,找能接受前缀的后缀,拼上就好,我们又知道一个前缀添加一个字符所能达到的状态是 nxt[p][c]。那么每种串的sum就可以用正向拓扑来转移,而且这里还有前导0不算的问题,就是拓扑的时候root->0 这个不入队就不会造成影响了。

#include <bits/stdc++.h>
using namespace std;
//http://blog.csdn.net/acm_cxlove/article/details/8234200
const int maxn = 200000+5;
int last,tail,Min[maxn];
int Max[maxn];
int nxt[maxn][11],fail[maxn];
int sz[maxn];

typedef long long ll;
const int mod = 2012;
void build(char *s)
{
while(*s)
{
int p=last,t=++tail,c=*s++-'0';
Max[t]=Max[p]+1;
memset(nxt[p],0,sizeof(nxt[p]));
while(p&&!nxt[p][c])
nxt[p][c]=t,p=fail[p];
if(p)
{
int q=nxt[p][c];
if(Max[q]==Max[p]+1)
fail[t]=q,Min[t]=Max[q]+1;
else
{
int k=++tail;
fail[k]=fail[q];
fail[t]=fail[q]=k;
Max[k]=Max[p]+1;
memcpy(nxt[k],nxt[q],sizeof(nxt[q]));
while(p&&nxt[p][c]==q)
nxt[p][c]=k,p=fail[p];
}
}
else
fail[t]=Min[t]=1;
last=t;
}
}

char s[200010];
int st[200010],in[200010];
int sum[200010];
int vis[200010];

int solve()
{
memset(vis,0,sizeof(vis));
memset(sum,0,sizeof(sum));
memset(in,0,sizeof(in));
memset(sz,0,sizeof(sz));
int ta=0,head=1;
sz[1]=1;
sum[1]=0;
st[++ta]=1;
vis[1]=1;
while(head<=ta)
{
int p=s
4000
t[head];
head++;
for(int i=0;i<=9;i++)
{
if(p==1&&i==0) continue;
if(nxt[p][i])
{
in[nxt[p][i]]++;
if(!vis[nxt[p][i]])
vis[nxt[p][i]]=1,st[++ta]=nxt[p][i];
}
}
}

head=1;
ta=0;
st[++ta]=1;
int ans=0;

while(head<=ta)
{
int p=st[head];
head++;
for(int i=0;i<=9;i++)
{
if(p==1&&i==0) continue;
if(nxt[p][i])
{
sz[nxt[p][i]]=(sz[nxt[p][i]]+sz[p])%mod;
sum[nxt[p][i]]=(sum[nxt[p][i]]+sum[p]*10+sz[p]*i)%mod;
--in[nxt[p][i]];
if(!in[nxt[p][i]])
st[++ta]=nxt[p][i];
}
}
}
for(int i=1;i<=tail;i++)
ans=(ans+sum[i])%mod;
return ans;
}

int main()
{
int n;
while(~scanf("%d",&n))
{
tail=1,last=1;
memset(nxt[1],0,sizeof(nxt[1]));
int len=0;
for(int i=1;i<=n;i++)
{
scanf("%s",s+len);
len=strlen(s);
s[len]='9'+1;
len++;
}
s[len]='\0';
build(s);
printf("%d\n", solve() );
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: