Codeforces Round #244 (Div. 2)D (后缀自己主动机)

(标号为0的节点一定是null节点,不管怎样都不能拿来用,切记切记,以后不能再错了)

这题用后缀自己主动机的话,对后缀自己主动机的非常多性质有足够深刻的理解。

没想过后缀数组怎么做。由于不高兴敲。。。

题意:给出两个长度均不超过5000的字符串s1,s2,求这两个串中,都仅仅出现一次的最短公共子串。

解题思路:求的是公共子串,然后对出现的次数又有限制,第一想法就是后缀自己主动机啊,后缀自己主动机处理子串出现次数再合适只是了。做法是这种。先建立s1的sam。用拓扑dp,求出每一个节点的代表串出现的次数。

目的是什么呢?事实上我是想求ok[i][j]。表示s1[i] ~ s1[j]的这个子串是否仅仅出现了一次。

如今我们求出了代表串的出现次数了。怎么求这个ok[i][j]呢?拿s1在建立好的自己主动机上匹配,当前匹配到了s1[i],记录temp表示当前匹配的最长长度。now表示当前匹配在哪个节点。这里有一个跟AC自己主动机非常相似的性质。匹配到了now,则一定能匹配fa[now]。

那么就顺着now往上走。一直找到第一个出现次数大于1的节点p,那么以i为结尾。长度为val[p]+1到temp的子串在s1里面肯定都仅仅出现一次了。把这个记录到ok数组里。
   第二步是对s2处理了。还是一样的过程,建立sam。求出每一个点的代表串出现的次数,即cnt[]数组。   第三步就要拿s1在s2的sam上进行匹配了,匹配过程类似于前面处理s1的ok数组,找出当前匹配的最长长度temp。匹配到的节点now,顺着now往上。找到第一个cnt大于1的节点p。在s2里面,以当前匹配上的子串的结尾为结尾的长度为val[p] + 1到temp的子,串必定仅仅在s2里出现过一次。

然后就枚举j,从val[p] + 1到temp,假设在s1里面。以i为结尾。长度为j的子串仅仅出现1次(即ok[i-j+1][i]
== 1)。那么这个j就有可能成为答案,用其更新ans就可以。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std ; const int maxn = 5001 ;
bool ok[maxn][maxn] ;
int ans = 111111 ; struct SAM {
int fa[maxn<<1] , val[maxn<<1] , c[26][maxn<<1] ;
int cnt[maxn<<1] ; int tot , last ;
int ws[maxn<<1] , wv[maxn<<1] ; inline int new_node ( int _val ) {
val[++tot] = _val ;
for ( int i = 0 ; i < 26 ; i ++ ) c[i][tot] = 0 ;
cnt[tot] = fa[tot] = 0 ;
return tot ;
} void add ( int k ) {
int p = last , i ;
int np = new_node ( val[p] + 1 ) ;
while ( p && !c[k][p] ) c[k][p] = np , p = fa[p] ;
if ( !p ) fa[np] = 1 ;
else {
int q = c[k][p] ;
if ( val[q] == val[p] + 1 ) fa[np] = q ;
else {
int nq = new_node ( val[p] + 1 ) ;
for ( i = 0 ; i < 26 ; i ++ )
c[i][nq] = c[i][q] ;
fa[nq] = fa[q] ;
fa[q] = fa[np] = nq ;
while ( p && c[k][p] == q ) c[k][p] = nq , p = fa[p] ;
}
}
last = np ;
} void init () {
tot = 0 ;
last = new_node ( 0 ) ;
} void SORT () {
for ( int i = 0 ; i < maxn ; i ++ ) wv[i] = 0 ;
for ( int i = 1 ; i <= tot ; i ++ ) wv[val[i]] ++ ;
for ( int i = 1 ; i < maxn ; i ++ ) wv[i] += wv[i-1] ;
for ( int i = 1 ; i <= tot ; i ++ ) ws[wv[val[i]]--] = i ;
} void get_cnt ( char *s , int n ) {
SORT () ;
int now = 1 , i ;
memset ( cnt , 0 , sizeof ( cnt ) ) ;
for ( i = 1 ; i <= n ; i ++ ) {
int k = s[i] - 'a' ;
now = c[k][now] ;
cnt[now] ++ ;
}
for ( i = tot ; i >= 1 ; i -- ) {
now = ws[i] ;
cnt[fa[now]] += cnt[now] ;
}
} void gao ( char *s , int n ) {
get_cnt ( s , n ) ;
int now = 1 , i , j ;
for ( i = 1 ; i <= n ; i ++ ) {
int k = s[i] - 'a' ;
now = c[k][now] ;
int p = now ;
while ( fa[p] && cnt[p] == 1 ) p = fa[p] ;
for ( j = 1 ; j <= i - val[p] ; j ++ )
ok[j][i] = 1 ;
}
} void work ( char *s , int n ) {
int temp = 0 , now = 1 , i , j ;
for ( i = 1 ; i <= n ; i ++ ) {
int k = s[i] - 'a' ;
if ( c[k][now] ) {
temp ++ ; now = c[k][now] ;
int p = now ;
while ( fa[p] && cnt[p] == 1 ) p = fa[p] ;
for ( j = val[p] + 1 ; j <= temp ; j ++ )
if ( ok[i-j+1][i] ) {
ans = min ( ans , j ) ;
break ;
}
}
else {
while ( now && !c[k][now] ) now = fa[now] ;
if ( !now ) now = 1 , temp = 0 ;
else {
temp = val[now] + 1 ;
now = c[k][now] ;
int p = now ;
while ( fa[p] && cnt[p] == 1 ) p = fa[p] ;
for ( j = val[p] + 1 ; j <= temp ; j ++ )
if ( ok[i-j+1][i] ) {
ans = min ( ans , j ) ;
break ;
}
}
}
}
} } ac ;
char s1[maxn] , s2[maxn] ; int main () {
scanf ( "%s" , s1 + 1 ) ;
ac.init () ;
int n = strlen ( s1 + 1 ) , i , j ;
for ( i = 1 ; i <= n ; i ++ )
ac.add ( s1[i] - 'a' ) ;
ac.gao ( s1 , n ) ;
scanf ( "%s" , s2 + 1 ) ;
ac.init () ;
int m= strlen ( s2 + 1 ) ;
for ( i = 1 ; i <= m ; i ++ )
ac.add ( s2[i] - 'a' ) ;
ac.get_cnt ( s2 , m ) ;
ac.work ( s1 , n ) ;
if ( ans == 111111 ) puts ( "-1" ) ;
else printf ( "%d\n" , ans ) ;
return 0 ;
}

Codeforces Round #244 (Div. 2)D (后缀自己主动机)的更多相关文章

  1. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  2. Codeforces Round #244 (Div. 2) C. Checkposts (tarjan 强连通分量)

    题目:http://codeforces.com/problemset/problem/427/C 题意:给你n座城市,m条有向道路,然后有一个机制,你在某一个城市设置检查点,那么被设置的检查点受保护 ...

  3. Codeforces Round #244 (Div. 2)

    今天是水题集啊.... A. Police Recruits time limit per test 1 second memory limit per test 256 megabytes inpu ...

  4. Codeforces Round #244 (Div. 2) B. Prison Transfer

    题目是选出c个连续的囚犯,而且囚犯的级别不能大于t #include <iostream> using namespace std; int main(){ int n,t,c; cin ...

  5. Codeforces Round #244 (Div. 2) A. Police Recruits

    题目的意思就是找出未能及时处理的犯罪数, #include <iostream> using namespace std; int main(){ int n; cin >> ...

  6. Codeforces Round #244 (Div. 2)——Checkposts

    题目链接 题意: 给定n个点,每一个点有一个权值的有向图.如今须要选定一些点,使得这些点权值和最小.且满足:假设i能到达j且j能到达i,那么i.j能够仅仅选一个 分析: 强联通模板题 //使用时仅仅更 ...

  7. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  8. Codeforces Round #110 (Div. 2)

    Codeforces Round #110 (Div. 2) C. Message 题意 给两个长度不超过2000的字符串\(s,u\),仅由小写字母构成. 找出\(s\)的一个子串\(t\),通过3 ...

  9. Codeforces Round #246 (Div. 2)

    题目链接:Codeforces Round #246 (Div. 2) A:直接找满足的人数,然后整除3就是答案 B:开一个vis数组记录每一个衣服的主场和客场出现次数.然后输出的时候主场数量加上反复 ...

随机推荐

  1. oj测试点相关 (整理摘编)

    Accepted                          通过!(AC) Wrong Answer                  答案错.(WA) Runtime Error      ...

  2. gdb打印vector

    1.gdb版本大于7.0 (gdb) p yourVector 2.打印整个vector (gdb) p *(yourVector._M_impl._M_start)@yourVector.size( ...

  3. Phoenix与Squirrel 是什么?

    不多说,直接上干货! 前言 Phoenix是HBase的开源SQL引擎. squirrel是windows上Phoneix可视化工具.  Phoenix的官网 http://phoenix.apach ...

  4. 导入不同业务数据通过Excel实现

    很多公司都用到了老系统移植到新系统,数据自然也需要迁移,这个解决方案之一就是使用Excel文件导入. 结合公司实现,然后简单写了个Demo. (PS:去找朋友本想着花几十分钟弄出来炫耀一波,结果花了三 ...

  5. spring jdbc、事务(三)

    spring整合jdbc spring中提供了一个可以操作数据库的对象(JDBCTemplate),对象封装了jdbc技术. 1.使用spring整合jdbc需要jdbc驱动.c3p0连接池.spri ...

  6. 在窗体中把DataGridView中的数据导出Excel

    //DataGridView导出Excel private void bt_Excl_Click(object sender, EventArgs e) { SaveFileDialog saveFi ...

  7. 多种效果进度指示层效果iOS源码项目

    该源码是一个多种效果进度指示层效果源码案例,源码KVNProgress,KVNProgress提供多种效果的进度指示层,有半透明效果,也支持全屏显示.指示层还高度支持自定义,可以按自己的需求定制.效果 ...

  8. Matlab移植到Eigen用到的词条

    同型矩阵运算满足加法交换律.结合律:并存在单位元.逆元.和0元,为同型矩阵对加法的交换环. Eigen的简单运算参考:http://blog.163.com/jiaqiang_wang/blog/st ...

  9. 从柯洁对战AlphaGo,看商业智能

    [摘要]李开复赛前说,AlphaGo和李世石的人机大战是第一次,可能还有悬念,那今天的AlphaGo已经在围棋的世界中彻底甩开了人类,不再拥有任何其他的可能.并指出,AlphaGo和柯洁的比赛并非没有 ...

  10. webstorm前端开发工具vue环境配置及运行项目

    1:webstorm的安装:2:node.js的安装3:安装Git4:vue-cli 安装前面两步就可以把项目启动了,安装Git主要是打开命令窗口,这样就可以用liunx命令了,原理跟cmd差不多 V ...