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. POJ 1118 求平面上最多x点共线

    题意:给你n个点的坐标.求一条直线最多能穿过多少个点. 思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成 //By: Sirius_Ren #include <cmath&g ...

  2. U - Three displays

    Problem description It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk ( ...

  3. windows phone数据网络开发

    LINQ LINQ的全称是Language INtegrated Query,即语言集成查询.LINQ是一种查询语言,不仅可以对数字库进行查询,还可以对.net的数据集.数组.Xml文档等对象进行查询 ...

  4. 【JAVA练习】- 一个逻辑题

    打印 1 3    4 5   8    12 7   12   20   32 9    16  28    48   80  ..... 输入任意一个奇数,输出那一行的数据 第一种方法找到规律进行 ...

  5. 自动换行 word-break:break-all和word-wrap:break-word

    1.word-break:break-all;当内容(比如很长的一个单词)到每行的末端时,它会把单词截断显示一部分,下一行显示后一部分. 2.word-wrap:break-word;当内容(比如很长 ...

  6. 【Oracle】服务器端监听配置

    一.静态监听 创建端口为1521的监听,静态注册,本机ip:192.168.10.2 [oracle@localhost ~]$ vi /u01/app/oracle/product/11.2.0/d ...

  7. SAP computer之architecture

    Simple-As-Possible computer introduces all the cruicial ideas behind computer operation without bury ...

  8. UI Testing

    UI Test能帮助我们去验证一些UI元素的属性和状态.Apple 在 Xcode 7 中新加入了一套 UI Testing 的工具,其目的就是解决自动化UI测试这个问题.新的 UI Testing ...

  9. python tkinter模块小工具界面

    代码 #-*-coding:utf-8-*- import os from tkinter import * root=Tk() root.title('小工具') #清空文本框内容 def clea ...

  10. loadrunner录制不了

    我在使用loadrunner过程中遇到的问题是,录制脚本时候能够打开firefox,但是无法打开IE,降低IE版本以后仍然不行. 1.在录制脚本时Start Recoding中,默认如下,这样有可能I ...