题目链接:Click here~~
题意:
给 n 个以数字构成的串,求这些串能组成的不重复的子串和(前导 0 也算重复)。(Σsi <= 1e5)
解题思路:
主流题解貌似都是用后缀自动机搞的。。。这么高端洋气的数据结构我是不太想学了,SA 就学了好几个月。TAT。
看括号里多么熟悉的条件啊,那就还是先用特殊字符将大家连起来。
(应该用不同的字符隔开,为了方便,我用了相同的字符,代价是开一个pos_end[],记录每个位置所属串的结尾位置)
跑完 height[] 后,还是按照之前的做法,统计不同子串,so 现在的关键问题是,如何在 O(1) 的时间内得到以某一后缀开头为前缀的某一段子串的和。
以字符串 "67890" 为例。
首先我维护一个 sum[] ,记录以字符串起始位置为前缀的子串前缀和。(即sum[0] = 6 , sum[1] = 6+67 , sum[2] = 6+67+678)
假设现在统计 "890" 这个后缀为开头的前缀,要得到 8+89+890 ,但 sum[] 里只保存了从 "6" 开始的和。
用 sum[j] - sum[i] 容易得到 678+6789+67890,然后减去 67*1110 即可。“67” 正是他前面字符串所组成的数字,而 1110 也不是空穴来风。。。
做法很好想了,再开一个 num[] 数组记录当前数字, table[] 记录 11...10 对 2012 的模。
这题调了一晚上加一上午。。。不过样例很强啊,过了就基本 1A 了。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 2e5 + 5;
int sa[N],rank[N],rank2[N],height[N],cnt[N],*x,*y;
/*
* a radix_sort which is based on the y[].
* how ? ahhhh, the last reverse for is the solution.
* and the adjacant value of sa[] might have the same rank.
*/
void radix_sort(int n,int sz)
{
memset(cnt,0,sizeof(int)*sz);
for(int i=0;i<n;i++)
cnt[ x[ y[i] ] ]++;
for(int i=1;i<sz;i++)
cnt[i] += cnt[i-1];
for(int i=n-1;i>=0;i--)
sa[ --cnt[ x[ y[i] ] ] ] = y[i];
}
/*
* sa[i] represents the ith suffix string is which one.
* rank[i] represents the suffix string [i,n]'s rank.
* sz is the max_rank of text in that time.
* x[] represents the true pointer of rank[] in that time and it may be not unique.
* y[] is the location of text[] which is sorted by 2nd key in that time before swap(x,y).
*/
void get_sa(char text[],int n,int sz=128)
{
x = rank, y = rank2;
for(int i=0;i<n;i++)
x[i] = text[i], y[i] = i;
radix_sort(n,sz);
for(int len=1;len<n;len<<=1)
{
int yid = 0;
for(int i=n-len;i<n;i++)
y[yid++] = i;
for(int i=0;i<n;i++)
if(sa[i] >= len)
y[yid++] = sa[i] - len;
radix_sort(n,sz);
swap(x,y);
x[ sa[0] ] = yid = 0;
for(int i=1;i<n;i++)
{
if(y[ sa[i-1] ]==y[ sa[i] ] && sa[i-1]+len<n && sa[i]+len<n && y[ sa[i-1]+len ]==y[ sa[i]+len ])
x[ sa[i] ] = yid;
else
x[ sa[i] ] = ++yid;
}
sz = yid + 1;
if(sz >= n)
break;
}
for(int i=0;i<n;i++)
rank[i] = x[i];
}
/*
* height[] represents the longest common prefix of suffix [i-1,n] and [i,n].
* height[ rank[i] ] >= height[ rank[i-1] ] - 1.
..... let's call [k,n] is the suffix which rank[k] = rank[i-1] - 1,
...=> [k+1,n] is a suffix which rank[k+1] < rank[i]
..... and the lcp of [k+1,n] and [i,n] is height[ rank[i] ] - 1.
..... still unknow ? height[ rank[i] ] is the max lcp of rank[k] and rank[i] which rank[k] < rank[i].
*/
void get_height(char text[],int n)
{
int k = 0;
for(int i=0;i<n;i++)
{
if(rank[i] == 0)
continue;
k = max(0,k-1);
int j = sa[ rank[i]-1 ];
while(i+k<n && j+k<n && text[i+k]==text[j+k])
k++;
height[ rank[i] ] = k;
}
}
const int mod = 2012;
char str[N],str2[N];
int pos_end[N],num[N],sum[N],table[N];
int main()
{
for(int i=1;i<N;i++)
table[i] = (table[i-1] * 10 + 10) % mod;
int m;
while(~scanf("%d",&m))
{
int n = 0;
while(m--)
{
scanf("%s",str2);
int next_pos = n + strlen(str2) - 1;
for(int i=0;str2[i];i++)
{
num[n+1] = (num[n] * 10 + str2[i] - '0') % mod;
sum[n+1] = (sum[n] + num[n+1]) % mod;
pos_end[n] = next_pos;
str[n++] = str2[i];
}
num[n+1] = 0;
sum[n+1] = 0;
str[n++] = '#';
}
str[n] = '\0';
get_sa(str,n);
get_height(str,n);
int ans = 0;
for(int i=0;i<n;i++)
{
int pos1 = sa[i] + height[i];
int pos2 = pos_end[sa[i]];
if(str[sa[i]] == '#' || str[sa[i]] == '0' || pos1 > pos2)
continue;
int sum1 = sum[pos1] - sum[sa[i]] - num[sa[i]] * table[pos1-sa[i]];
int sum2 = sum[pos2+1] - sum[sa[i]] - num[sa[i]] * table[pos2-sa[i]+1];
(ans += sum2 - sum1) %= mod;
(ans += mod) %= mod;
}
printf("%d\n",ans);
}
return 0;
}