后缀数组hdu4436(2012现场赛)

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

str2int

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


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


题意:告诉你n个数字字符串,问他的不同的子串转化成数字之后的和对2012取余

思路:先把对所有的串拼接起来,然后求后缀数组。求完后如何求和那?参考了大神的解题思路:要在i+height[rank[i]]~第i个串的结束位置,因为前面的必然重复了,但也不能超过结束位置。计算的话就是维护一个部分和,然后把需要减去的剪掉即可。程序中维护了一个T数组和V数组。如1234   T[4]=1+12+123+1234  V[2]=12   计算3+34 = T[4]-T[2]-V[2]*110 即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=200100;
const int MOD=2012;

int sa[maxn],height[maxn],rank[maxn],t[maxn],t2[maxn],c[maxn];
int arr[maxn],belong[maxn],T[maxn],V[maxn],Pow[maxn],len[maxn];
int str[maxn];
int N,n;

void build_sa(int m,int l)
{
    int *x=t,*y=t2;
    for(int i=0;i<m;i++)c[i]=0;
    for(int i=0;i<l;i++)c[x[i]=str[i]]++;
    for(int i=1;i<m;i++)c[i]+=c[i-1];
    for(int i=l-1;i>=0;i--)sa[--c[x[i]]]=i;
    for(int k=1;k<=l;k<<=1)
    {
        int p=0;
        for(int i=l-k;i<l;i++)y[p++]=i;
        for(int i=0;i<l;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
        for(int i=0;i<m;i++)c[i]=0;
        for(int i=0;i<l;i++)c[x[y[i]]]++;
        for(int i=1;i<m;i++)c[i]+=c[i-1];
        for(int i=l-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
        swap(x,y);
        x[sa[0]]=0;p=1;
        for(int i=1;i<l;i++)
            x[sa[i]]=((y[sa[i-1]]==y[sa[i]])&&(y[sa[i-1]+k]==y[sa[i]+k])?p-1:p++);
        if(p>=l)break;
        m=p;
    }
}

void init()
{
    memset(Pow,0,sizeof(Pow));
    Pow[0]=1;
    Pow[1]=10;
    for(int i=2;i<maxn;i++)
        Pow[i]=(Pow[i-1]+1)*10%MOD;
}

void getheight(int n)
{
    int k=0;
    for(int i=1;i<=n;i++)rank[sa[i]]=i;
    for(int i=0;i<n;i++)
    {
        if(k)k--;
        int j=sa[rank[i]-1];
        while(str[i+k]==str[j+k]){k++;}
        height[rank[i]]=k;
    }
}

void add(int x,int &value,int i)
{
    str[n]=x;
    value=(value*10+str[n]-1)%MOD;
    T[n+1]=(T[n]+value)%MOD;
    V[n+1]=value;
    belong[n]=i;
    n++;
}

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

void solve()
{
    int ans=0;
    for(int i=0;i<n;i++)
    {
        if(str[i]%10!=1)//当str[i]为1或11时不用算
        {
            if(i+height[rank[i]]<=arr[belong[i]])
            {
                ans+=cal(i,arr[belong[i]])-cal(i,i+height[rank[i]]-1);
                ans%=MOD;
                if(ans<0)ans+=MOD;
            }
        }
    }
    printf("%d\n",ans);
}

int main()
{
    init();
    while(scanf("%d",&N)!=EOF)
    {
        char word[maxn];
        n=0;
        int value=0;
        for(int i=1;i<=N;i++)
        {
            scanf("%s",word);
            len[i]=strlen(word);

            for(int j=0;j<len[i];j++)
                add(word[j]-'0'+1,value,i);
            add(11,value,i);
            arr[i]=n-2;
        }
        str[n-1]=0;
        build_sa(12,n);
        getheight(n-1);
        solve();
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值