hdu 4436 str2int(后缀数组)

str2int



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   |   We have carefully selected several similar problems for you:   5634  5633  5632  5629  5628 

如1234   T[4]=1+12+123+1234  V[2]=12   计算3+34 = T[4]-T[2]-V[2]*110 即可。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#define N 300000
#define MOD 2012
using namespace std;
int wa[N],wb[N],wv[N];
int rank[N],height[N],ws[N],sa[N],P[N],pow[N],V[N],
T[N],Len[N],S[N],arrive[N],n,len,value;
char s[N];
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(p=1,j=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++);
    return;
}
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()
{
    freopen("C:\\Users\\Administrator\\Desktop\\input.txt","r",stdin);
    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)
    {
        len=0;value=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            Len[i]=strlen(s);
            for(int j=0;j<Len[i];j++)
            {
                S[len]=s[j]-'0'+1;
                value=(value*10+S[len]-1)%MOD;
                V[len+1]=value;
                T[len+1]=(T[len]+value)%MOD;
                P[len]=i;
                len++;
            }
            S[len]=11;//设置分隔符
            value=(value*10+10)%MOD;
            V[len+1]=value;
            T[len+1]=(T[len]+value)%MOD;
            P[len]=i;
            len++;
            arrive[i]=len-2;
        }
        S[len-1]=0;//增加末尾的0
        da(S,sa,len,12);
        calheight(S,sa,len);
        int ans=0;
        for(int i=0;i<len;i++)
        {
            if(S[i]%10!=1)
            {
                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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值