(http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105904#problem/C)
题意:n个串由’0’~’9’组成,问所有不相同子串对应的十进制数之和。
解法:把所有的串连起来,构造好后缀自动机,xjb推个公式就好了。要注意的是,子串不允许出现前导0,所以在源点那里不要转移0就好了。还有就是处理的时候要先对节点按step做一遍topo排序。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)
const int maxn=2*100000+30;
const int mod =2012;
struct suffix_automaton{
int son[maxn][14],pre[maxn],step[maxn];
int last,si;
void pushback(int v)
{
step[++si]=v;
pre[si]=-1;
clr(son[si],-1);
}
void Extend(int ch)
{
pushback(step[last]+1);
int p=last,np=si;
while(p!=-1&&son[p][ch]==-1)
son[p][ch] = np , p = pre[p];
if(p==-1) pre[np]=0;
else{
int q = son[p][ch];
if(step[q]==step[p]+1) pre[np]=q;
else{
pushback(step[p]+1);
int nq=si;
memcpy(son[nq],son[q],sizeof son[q]);
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
while(p!=-1&&son[p][ch]==q)
son[p][ch]=nq,p=pre[p];
}
}
last=np;
}
void init()
{
si=last=0;
clr(son[0],-1);
pre[0]=-1;
step[0]=0;
}
int cnt[maxn],pos[maxn];
void cca()
{
clr(cnt,0);
for(int i=0;i<=si;i++)
cnt[step[i]]++;
for(int i=1;i<=si;i++)
cnt[i]+=cnt[i-1];
for(int i=0;i<=si;i++)
pos[--cnt[step[i]]]=i;
}
int a[maxn],b[maxn];
int solve()
{
cca();
for(int q=si;q>=0;q--)
{
int p=pos[q];
a[p]=0;
b[p]=1;
for(int i=0;i<10;i++) if( son[p][i] != -1 )
{
if(p==0&&i==0) continue;
//printf("p %d i %d %d\n",p,i,son[p][i]);
a[p] = ( a[p] + a[son[p][i]] + b[son[p][i]]*i ) % mod;
b[p] = ( b[p] + b[son[p][i]]*10 ) % mod;
}
//printf("p %d a %d b %d\n",p,a[p],b[p]);
}
return a[0];
}
}su;
char s[maxn];
int n;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("out2.txt","w",stdout);
while(~scanf("%d",&n)){
su.init();
for(int i=0;i<n;i++)
{
scanf("%s",s);
for(int i=0;s[i];i++)
su.Extend(s[i]-'0');
su.Extend(11);
}
printf("%d\n",su.solve());
}
return 0;
}