关闭

【后缀数组】Str2int

标签: Hdu
502人阅读 评论(0) 收藏 举报
分类:

str2int

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


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
 

Recommend
zhoujiaqi2010
 
 
可以采用后缀数组或者后缀自动机。我用的后缀数组。
用后缀数组跑一遍求出Height数组后。
 
排除有前导零的后缀。
用T[i] 表示后缀i的所有前缀的和。V[i]表示前缀i。
两个不同后缀前缀和的差T[i] - T[j-1] 表示结尾是从j到i的前缀和。
但是我们不仅是结尾从j到i,开头也有限制,开头是从j开始的,所以要去掉高位。
观察发现,对于每个前缀,高位的数字是一样的,末尾的零的个数不一样,所以我们在前缀和中减去高位的数字和111111....110的乘积。
 
如1234。 T[4]=1+12+123+1234  V[2]=12   计算3+34 = T[4]-T[2]-V[2]*110
 
 
然后就是Height数组派上用场的地方:
我们把以上的和相加会发现,总是会多加一部分。
因为后缀数组中没有了相同的后缀,但是这些后缀存在很多相同的前缀。于是我们要减去多加的那一部分(当然是先计算短的,再计算长的,再在长的里面减去重复计算的那部分)
 
#include <cstdio>
#include <cstring>
#define REP(i,n) for(int i=0;i<(n);i++)
const int maxn = 300000;
const int MOD = 2012;

int sa[maxn],height[maxn],rank[maxn],V[maxn],T[maxn],arrive[maxn],Pow[maxn],P[maxn];

int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void DA(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for (i=0;i<m;i++) ws[i]=0;
    for (i=0;i<n;i++) ws[x[i]=r[i]]++;
    for (i=1;i<m;i++) ws[i]+=ws[i-1];
    for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
    for (j=1,p=1;p<n;j*=2,m=p)
    {
        for (p=0,i=n-j;i<n;i++) y[p++] = i;
        for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
        for (i=0;i<n;i++) wv[i]=x[y[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--) sa[--ws[wv[i]]]=y[i];
        for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
}
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for (i=0;i<n;++i) rank[sa[i]] = i;
    for (i=0;i<n;height[rank[i++]] = k)
        if (rank[i]) for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];++k);
}

char tmp[maxn];
int str[maxn];

int get(int l,int r)
{
    if (l > r) return 0;
    int ans = (T[r+1]-T[l])%MOD;
    ans -= V[l]*(Pow[r-l+1]);
    ans %= MOD;
    if (ans<0) ans += MOD;
    return ans;
}

int main()
{
    int n;

    Pow[0] = 1; Pow[1] = 10;
    for (int i=2;i<200000;++i)
        Pow[i] = (Pow[i-1]+1)*10%MOD;

    while (scanf("%d",&n)!=EOF)
    {
        int len = 0;
        int val = 0;
        REP(i,n)
        {
            scanf("%s",tmp);
            int tl = strlen(tmp);
            REP(j,tl)
            {
                str[len++] = tmp[j]-'0'+1;
                val = (val*10+str[len-1]-1)%MOD;
                P[len-1] = i;
                V[len] = val;
                T[len] = (T[len-1]+val)%MOD;
            }
            str[len++] = 11;
            P[len-1] = 11;
            V[len] = val;
            T[len] = (T[len-1]+val)%MOD;
            arrive[i] = len-2;
        }
        str[len-1] = 0;

        DA(str,sa,len,12);
        calheight(str,sa,len);

        int ans = 0;
        REP(i,len)
        {
            if (str[i]!=1 && str[i]!=11)
            if (i+height[rank[i]]<=arrive[P[i]])
            {
                ans += get(i,arrive[P[i]])-get(i,i+height[rank[i]]-1);
                ans %= MOD; if (ans < 0) ans += MOD;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:335959次
    • 积分:7288
    • 等级:
    • 排名:第3142名
    • 原创:419篇
    • 转载:14篇
    • 译文:0篇
    • 评论:32条
    文章分类
    最新评论