[题目链接]

https://codeforces.com/contest/452/problem/E

[算法]

构建后缀数组

用并查集合并答案即可

时间复杂度 : O(NlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 3e5 + ;
const int P = 1e9 + ; #define rint register int struct info
{
int id , ht;
} a[N]; int n;
int height[N] , sa[N] , rk[N] , sz[N] , cnta[N] , cntb[N] , cntc[N] , bel[N] , fa[N];
ll ans[N];
char s[N] , s1[N] , s2[N] , s3[N]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void add(T &x , T y)
{
x += y;
while (x >= P) x -= P;
}
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void build_sa()
{
static int x[N] , y[N] , cnt[N];
for (rint i = ; i <= n; ++i) ++cnt[s[i]];
for (rint i = ; i <= ; ++i) cnt[i] += cnt[i - ];
for (rint i = n; i >= ; --i) sa[cnt[s[i]]--] = i;
rk[sa[]] = ;
for (rint i = ; i <= n; ++i) rk[sa[i]] = rk[sa[i - ]] + (s[sa[i]] != s[sa[i - ]]);
for (rint k = ; rk[sa[n]] != n; k <<= )
{
for (rint i = ; i <= n; ++i)
x[i] = rk[i] , y[i] = (i + k <= n) ? rk[i + k] : ;
for (rint i = ; i <= n; ++i) cnt[i] = ;
for (rint i = ; i <= n; ++i) ++cnt[y[i]];
for (rint i = ; i <= n; ++i) cnt[i] += cnt[i - ];
for (rint i = n; i >= ; i--) rk[cnt[y[i]]--] = i;
for (rint i = ; i <= n; ++i) cnt[i] = ;
for (rint i = ; i <= n; ++i) ++cnt[x[i]];
for (rint i = ; i <= n; ++i) cnt[i] += cnt[i - ];
for (rint i = n; i >= ; i--) sa[cnt[x[rk[i]]]--] = rk[i];
rk[sa[]] = ;
for (rint i = ; i <= n; ++i) rk[sa[i]] = rk[sa[i - ]] + (x[sa[i]] != x[sa[i - ]] || y[sa[i]] != y[sa[i - ]]);
}
}
inline void get_height()
{
int k = ;
for (rint i = ; i <= n; ++i)
{
if (k) --k;
int j = sa[rk[i] + ];
while (s[i + k] == s[j + k]) ++k;
height[rk[i]] = k;
}
}
inline bool cmp(info a , info b)
{
return a.ht > b.ht;
}
inline int get_root(int x)
{
if (fa[x] == x) return x;
else return fa[x] = get_root(fa[x]);
}
inline void merge(int x , int y)
{
if (sz[x] > sz[y]) swap(x , y);
fa[x] = y;
cnta[y] += cnta[x];
cntb[y] += cntb[x];
cntc[y] += cntc[x];
sz[y] += sz[x];
return;
}
inline int _min(int x , int y , int z)
{
return min(min(x , y) , z);
} int main()
{ scanf("%s%s%s" , s1 + , s2 + , s3 + );
int l1 = strlen(s1 + ) , l2 = strlen(s2 + ) , l3 = strlen(s3 + );
for (rint i = ; i <= l1; ++i)
{
s[++n] = s1[i];
bel[n] = ;
}
s[++n] = '#';
for (rint i = ; i <= l2; ++i)
{
s[++n] = s2[i];
bel[n] = ;
}
s[++n] = '@';
for (rint i = ; i <= l3; ++i)
{
s[++n] = s3[i];
bel[n] = ;
}
build_sa();
get_height();
for (rint i = ; i < n; ++i)
{
a[i].id = i;
a[i].ht = height[i];
}
sort(a + , a + n , cmp);
for (rint i = ; i <= n; ++i)
{
fa[i] = i;
sz[i] = ;
if (bel[sa[i]] == ) cnta[i] = ;
if (bel[sa[i]] == ) cntb[i] = ;
if (bel[sa[i]] == ) cntc[i] = ;
}
for (rint i = ; i < n; ++i)
{
int rk = a[i].id , ht = a[i].ht;
if (!ht) break;
int fx = get_root(rk) , fy = get_root(rk + );
add(ans[ht] , 1LL * cnta[fx] * cntb[fx] % P * cntc[fy] % P);
add(ans[ht] , 1LL * cnta[fx] * cntc[fx] % P * cntb[fy] % P);
add(ans[ht] , 1LL * cntb[fx] * cntc[fx] % P * cnta[fy] % P);
add(ans[ht] , 1LL * cnta[fy] * cntb[fy] % P * cntc[fx] % P);
add(ans[ht] , 1LL * cnta[fy] * cntc[fy] % P * cntb[fx] % P);
add(ans[ht] , 1LL * cntb[fy] * cntc[fy] % P * cnta[fx] % P);
merge(fx , fy);
}
for (rint i = n; i >= ; --i) add(ans[i] , ans[i + ]);
for (rint i = ; i <= _min(l1 , l2 , l3); ++i) printf("%lld " , ans[i]);
printf("\n"); return ; }

[Codeforces 452E] Three Strings的更多相关文章

  1. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

  2. Codeforces 452E Three strings 字符串 SAM

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html 题目传送门 - CF452E 题意 给定三个字符串 $s1,s2,s3$ ,对于所有 $L ...

  3. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  4. codeforces 112APetya and Strings(字符串水题)

    A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. [Codeforces Round #438][Codeforces 868D. Huge Strings]

    题目链接:868D - Huge Strings 题目大意:有\(n\)个字符串,\(m\)次操作,每次操作把两个字符串拼在一起,并询问这个新串的价值.定义一个新串的价值\(k\)为:最大的\(k\) ...

  6. [Educational Round 5][Codeforces 616F. Expensive Strings]

    这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...

  7. Codeforces 559B - Equivalent Strings

    559B - Equivalent Strings 思路:字符串处理,分治 不要用substr(),会超时 AC代码: #include<bits/stdc++.h> #include&l ...

  8. Codeforces 112A-Petya and Strings(实现)

    A. Petya and Strings time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. CodeForces - 985F Isomorphic Strings

    假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了. 显然集合可以直接hash, ...

随机推荐

  1. DataTable去除空行

    protected void removeEmpty(DataTable dt) { List<DataRow> removelist = new List<DataRow>( ...

  2. Chrome + Python 抓取动态网页内容

    用Python实现常规的静态网页抓取时,往往是用urllib2来获取整个HTML页面,然后从HTML文件中逐字查找对应的关键字.如下所示: import urllib2 url="http: ...

  3. 02-cookie案例-显示用户上次访问网站的时间

    package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...

  4. ThinkPHP3.1在多数据库连接下存储过程调用bug修正

    最近使用ThinkPHP3.1进行一个项目的开发,由于该项目需要连接多台不同的数据库,所以使用如下配置方法: <?php return array( //'配置项'=>'配置值' //数据 ...

  5. Web网页开发常见经典问题

    1.网络请求参数共享 转发dispatcher和重定向redirect 对于参数共享的区别 Redirect和Dispatcher 区别

  6. django数据库同步时报错“Table 'XXX' already exists”

    转自:http://blog.csdn.net/huanhuanq1209/article/details/77884014 执行manage.py makemigrations 未提示错误信息, 但 ...

  7. 初识vue-01

    一.属性和方法 vue自定义的一些数据和方法需要绑定到实例的不同属性上面去例如数据都要绑定要data属性,方法都要绑定到methods方法实例上的data和methods里面的key值会自动挂载到vu ...

  8. Android setTag()与getTag(),与set多个setTag()

    首先我们要知道setTag方法是干什么的,SDK解释为 Tags Unlike IDs, tags are not used to identify views. Tags are essential ...

  9. C语言--循环结构

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  10. PHP CURL 中文说明

    1.CURL是利用URL语法在命令行方式下工作的开源文件传输工具. 2.它被广泛应用在Unix.多种Linux发行版中.而且有DOS和Win32.Win64下的移植版本号. 3.它支持非常多协议:FT ...