hdu 4436 后缀自动机算和

本文介绍了一种算法,用于解决将多个仅包含数字的字符串转换为整数,并计算这些整数的总和,最终结果对2012取余。通过构建后缀自动机并利用正向拓扑的方法来避免重复计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 
Input
There 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. 
Output
An 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.youkuaiyun.com/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=st[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() );
    }
}  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值