http://acm.hdu.edu.cn/showproblem.php?pid=5558

题意:

当前的位置是 i , 就找到 s1 = ( 以i为起点到 len 的连续串 ) , s2=( 在 [0,i)内选一个起点到 len 的连续串) , 要求s1==s2 ; 如果有输出( 最大的长度 , 在[0,i)内选取的起点 ) , 如果找不到就输出(-1 ,ASCLL(str[i]) )

分析:

利用SAM的联机特性 , 一边插入一边找答案 , 还要求最小的下标就多维护一个first_endpos

就是找到一个最大的某后缀 , 比如:从起始状态不断的用当前字符str[i] , 往后面转移 , 不断的p=trans[p][str[i]] , 就是说找到str[i]的最大后缀

#include <bits/stdc++.h>
#define LL long long
#define P pair<int, int>
#define lowbit(x) (x & -x)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, n) for (int i = a; i <= n; ++i)
const int maxn = 1e6+;
#define mid ((l + r) >> 1)
#define lc rt<<1
#define rc rt<<1|1
using namespace std;
// __int128 read() { __int128 x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f;}
// void print(__int128 x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) print(x / 10); putchar(x % 10 + '0');} int len;
struct SAM{ int trans[maxn<<][], slink[maxn<<], maxlen[maxn<<];
// 用来求endpos
int endpos[maxn<<];
// 计算所有子串的和(0-9表示)
int last, now, root; inline void newnode (int v) {
maxlen[++now] = v;
mem(trans[now],);
} inline void extend(int c,int i) {
newnode(maxlen[last] + );
int p = last, np = now;
endpos[np]=i;
// 更新trans
while (p && !trans[p][c]) {
trans[p][c] = np;
p = slink[p];
}
if (!p) slink[np] = root;
else {
int q = trans[p][c];
if (maxlen[p] + != maxlen[q]) {
// 将q点拆出nq,使得maxlen[p] + 1 == maxlen[q]
newnode(maxlen[p] + );
int nq = now;
endpos[nq]=endpos[q];
memcpy(trans[nq], trans[q], sizeof(trans[q]));
slink[nq] = slink[q];
slink[q] = slink[np] = nq;
while (p && trans[p][c] == q) {
trans[p][c] = nq;
p = slink[p];
}
}else slink[np] = q;
}
last = np;
// 初始状态为可接受状态 } inline void init()
{
root = last = now = ;
slink[root]=;
mem(trans[root],);
//endpos[root]=0;
} }sam; int main()
{
//printf("%d ",maxn);
int t;scanf("%d",&t);
for(int w= ; w<=t ; w++){ string T;cin>>T;
sam.init();
len=T.size();printf("Case #%d:\n",w);
for(int i= ; i<len ; )
{
int p=sam.root , k=;
while(i < len && sam.trans[p][T[i]-'a'])
{
p=sam.trans[p][T[i]-'a'];
sam.extend(T[i]-'a',i);
k++ , i++;
}
//printf("%d\n",p);
if(k) printf("%d %d\n",k,sam.endpos[p]-k+);
else
{
sam.extend(T[i]-'a',i);
printf("-1 %d\n",(int)T[i]);
i++;
}
} }
//- sam.all();
}

HDU5558(后缀自动机~在本串中找前一最长子相同串)的更多相关文章

  1. java实现串中找数字

    串中找数字 以下的静态方法实现了:把串s中第一个出现的数字的值返回. 如果找不到数字,返回-1 例如: s = "abc24us43" 则返回2 s = "82445ad ...

  2. Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)

    题目:给定一个字符串S(主串),一个字符串数组words,其中的字符串的长度相同.找到所有的子串位置,要求是words中字符串的一个连接: 举例: For example, given:s: &quo ...

  3. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  4. hdu 4763 Theme Section(next数组找串中三段相等)

    题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...

  5. KMP小扩展,找出子串在主串中出现的所有位置

    KMP算法能够高效地匹配字符串,找出子串(T串)在主串(S串)中出现的首个位置的原算法网上已经有很多优秀的博文进行详细讲解,这里就不多赘述. 这篇博文主要是对KMP原算法稍作改动,使其能够在主串中把所 ...

  6. 后缀自动机 && 题目

    因为明天要讲解后缀自动机了,所以只能抱抱佛脚,临时做做题目.其实很久以前看过,但是不太懂,看的是clj的原文,不太懂.现在只能临时看看是怎么弄的,应付下. ---------------------- ...

  7. D. Match & Catch 后缀自动机 || 广义后缀自动机

    http://codeforces.com/contest/427/problem/D 题目是找出两个串的最短公共子串,并且在两个串中出现的次数只能是1次. 正解好像是dp啥的,但是用sam可以方便很 ...

  8. 后缀自动机(SAM)奶妈式教程

    后缀自动机(SAM) 为了方便,我们做出如下约定: "后缀自动机" (Suffix Automaton) 在后文中简称为 SAM . 记 \(|S|\) 为字符串 \(S\) 的长 ...

  9. BZOJ 后缀自动机四·重复旋律7

    后缀自动机四·重复旋律7 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的 ...

随机推荐

  1. Python爬虫入门六之Cookie的使用

    大家好哈,上一节我们研究了一下爬虫的异常处理问题,那么接下来我们一起来看一下Cookie的使用. 为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在 ...

  2. array_combine()

  3. Use formatter to format your JAVA code

    In order to make the codes looks unified and make it easy to understand, it's better to use the same ...

  4. 实践作业4:Web测试实践(小组作业)每日任务记录3

    会议时间:2017年12月23日 会议地点:东九教学楼自习区 主  持  人:王晨懿 参会人员:王晨懿.余晨晨.郑锦波.杨潇.侯欢.汪元 记  录  人:王晨懿 会议议题:小组作业第二阶段 下面是今天 ...

  5. Pandas -- Merge,join and concatenate

    Merge, join, and concatenate pandas provides various facilities for easily combining together Series ...

  6. ArcGIS 桌面远程连接带有端口号的SDE

    首先配置远程连接 PostgreSQL数据库远程连接功能的开启   需要修改连个配置文件,默认位于 安装目录的data子文件夹下.   1.postgresql.conf 修改成监听所有ip地址的连接 ...

  7. 淘宝IP地址库

    淘宝官方ip地址库 http://ip.taobao.com/ 接口说明 1. 请求接口(GET): http://ip.taobao.com/service/getIpInfo.php?ip=[ip ...

  8. Transaction And Lock--死锁错误号1205

    在TSQL中,如果需要判断当前错误是否是因为死锁引起,可以使用ERROR_NUMBER()=1205来判断在C#中,使用SQLException来捕获 SQLException.Number=1205 ...

  9. .net core 使用redis 基于 StackExchange.Redis

    一.添加引用包 StackExchange.Redis Microsoft.Extensions.Configuration   二.修改配置文件 appsettings.json   { " ...

  10. PT, BPT, VCM

    - VCM可以理解为BPT与PPM之间的MIS,并没有比BPT效果好很多 - BPT, VCM这类算法的主要作用是加快caustics的收敛,对indirect diffuse没有很明显的加速 - i ...