多串后缀自动机(hdu4436)

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
Author ID 
Password 
  Register new ID
【比赛提醒】BestCoder 你报名了吗?(点击报名) 
【科普】什么是BestCoder?如何参加?

str2int

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


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


对多个串建立后缀自动机,中间用10间隔,然后按照len进行排序,然后进行递推

记录两个值,cnt表示有多少个转移到该节点的路径,也就是要计算多少次,sum表示和

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=200010;
const int SIGMA_SIZE=11;
const int MOD=2012;
int N;
char s[maxn];
struct SAM_Node
{
    SAM_Node *par,*next[SIGMA_SIZE];
    int len,id,pos,cnt,sum;
    SAM_Node(){}
    SAM_Node(int _len)
    {
        par=0;
        len=_len;
        cnt=sum=0;
        memset(next,0,sizeof(next));
    }
};
SAM_Node node[maxn*2],*root,*last;
int SAM_size;
SAM_Node *newSAM_Node(int len)
{
    node[SAM_size]=SAM_Node(len);
    node[SAM_size].id=SAM_size;
    return &node[SAM_size++];
}
SAM_Node *newSAM_Node(SAM_Node *p)
{
    node[SAM_size]=*p;
    node[SAM_size].id=SAM_size;
    return &node[SAM_size++];
}
void SAM_add(int x)
{
    SAM_Node *p=last,*np=newSAM_Node(p->len+1);
    last=np;
    while(p&&!p->next[x])
    {
        p->next[x]=np;
        p=p->par;
    }
    if(!p)
        np->par=root;
    else
    {
        SAM_Node *q=p->next[x];
        if(q->len==p->len+1)
            np->par=q;
        else
        {
            SAM_Node *nq=newSAM_Node(q);
            nq->len=p->len+1;
            q->par=nq;
            np->par=nq;
            while(p&&p->next[x]==q)
            {
                p->next[x]=nq;
                p=p->par;
            }
        }
    }
}
void SAM_init()
{
    SAM_size=0;
    root=last=newSAM_Node(0);
    node[0].pos=0;
}
int cnt[maxn*2];
SAM_Node *sa[maxn*2];
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        SAM_init();
        for(int i=0;icnt=1;
        for(int i=0;inext[j])
                {
                    SAM_Node *p=tmp->next[j];
                    p->cnt=(p->cnt+tmp->cnt)%MOD;
                    p->sum=(p->sum+tmp->sum*10+tmp->cnt*j)%MOD;
                }
            }
            ans=(ans+tmp->sum)%MOD;
        }
        printf("%d\n",ans);
    }
    return 0;
}






你可能感兴趣的:(后缀自动机SAM)