[hdu4436]str2int

str2int

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1208    Accepted Submission(s): 411


Problem Description
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
 

Source
2012 Asia Tianjin Regional Contest

看了别人的题解才有了思路...
首先算出所有子串的和,维护一个sum数组,可以O(n)内得出答案
然后问题就是减去重复子串的和了,首先要找出重复子串,这里用到了后缀数组
把所有字符串连起来然后跑后缀数组,重复的串都是相邻两个的LCP


#include<cstdio>
#include<iostream>
#include<cstring>
#define maxn 400010
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
const int mod=2012;
char str1[maxn*3],str2[maxn*3];
int pend[maxn],num[maxn],sum[maxn],table[maxn*3],r[maxn*3],sa[maxn*3];
int c0(int *r,int a,int b)
{
    return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b)
{
    if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
    else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void sort(int *r,int *a,int *b,int n,int m)
{
    int i;
    for(i=0; i<n; i++) wv[i]=r[a[i]];
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[wv[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) b[--ws[wv[i]]]=a[i];
    return;
}
void dc3(int *r,int *sa,int n,int m)
{
    int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    r[n]=r[n+1]=0;
    for(i=0; i<n; i++) if(i%3!=0) wa[tbc++]=i;
    sort(r+2,wa,wb,tbc,m);
    sort(r+1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1; i<tbc; i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0; i<tbc; i++) san[rn[i]]=i;
    for(i=0; i<tbc; i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    sort(r,wb,wa,ta,m);
    for(i=0; i<tbc; i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0; i<ta && j<tbc; p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(; i<ta; p++) sa[p]=wa[i++];
    for(; j<tbc; p++) sa[p]=wb[j++];
    return;
}
int rank[maxn*3],height[maxn*3];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
    return;
}

int main()
{
    //freopen("C:\\Users\\Administrator\\Desktop\\code\\hdoj\\date\\out","r",stdin);
    table[1]=10;
    for (int i=2; i<=maxn; i++)
        table[i]=(table[i-1]*10+10)%mod;
    int m;
    while (scanf("%d",&m)==1)
    {
        int n=0;
        while(m--)
        {
            scanf("%s",str2);
            int next=n+strlen(str2)-1;
            for (int i=0; str2[i]; i++)
            {
                num[n+1]=(num[n]*10+str2[i]-'0')%mod;
                sum[n+1]=(sum[n]+num[n+1])%mod;
                pend[n]=next;
                str1[n++]=str2[i];
            }
            num[n+1]=0;
            sum[n+1]=0;
            str1[n++]='&';
        }
        str1[n]='/0';
        for (int i=0; i<=n; i++)
            r[i]=str1[i];
        r[n]=0;
        dc3(r,sa,n+1,128);
        calheight(r,sa,n);
        int ans=0;
        for (int i=1; i<=n; i++)
        {
            int p1=sa[i]+height[i];
            int p2=pend[sa[i]];
            if (str1[sa[i]]=='&'||str1[sa[i]]=='0'||p1>p2) continue;
            int sum1=sum[p1]-sum[sa[i]]-num[sa[i]]*table[p1-sa[i]];
            int sum2=sum[p2+1]-sum[sa[i]]-num[sa[i]]*table[p2-sa[i]+1];
            (ans += sum2 - sum1) %= mod;
            (ans += mod) %= mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:([hdu4436]str2int)