HDU5558(后缀自动机~在本串中找前一最长子相同串)
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(后缀自动机~在本串中找前一最长子相同串)的更多相关文章
- java实现串中找数字
串中找数字 以下的静态方法实现了:把串s中第一个出现的数字的值返回. 如果找不到数字,返回-1 例如: s = "abc24us43" 则返回2 s = "82445ad ...
- Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)
题目:给定一个字符串S(主串),一个字符串数组words,其中的字符串的长度相同.找到所有的子串位置,要求是words中字符串的一个连接: 举例: For example, given:s: &quo ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- hdu 4763 Theme Section(next数组找串中三段相等)
题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...
- KMP小扩展,找出子串在主串中出现的所有位置
KMP算法能够高效地匹配字符串,找出子串(T串)在主串(S串)中出现的首个位置的原算法网上已经有很多优秀的博文进行详细讲解,这里就不多赘述. 这篇博文主要是对KMP原算法稍作改动,使其能够在主串中把所 ...
- 后缀自动机 && 题目
因为明天要讲解后缀自动机了,所以只能抱抱佛脚,临时做做题目.其实很久以前看过,但是不太懂,看的是clj的原文,不太懂.现在只能临时看看是怎么弄的,应付下. ---------------------- ...
- D. Match & Catch 后缀自动机 || 广义后缀自动机
http://codeforces.com/contest/427/problem/D 题目是找出两个串的最短公共子串,并且在两个串中出现的次数只能是1次. 正解好像是dp啥的,但是用sam可以方便很 ...
- 后缀自动机(SAM)奶妈式教程
后缀自动机(SAM) 为了方便,我们做出如下约定: "后缀自动机" (Suffix Automaton) 在后文中简称为 SAM . 记 \(|S|\) 为字符串 \(S\) 的长 ...
- BZOJ 后缀自动机四·重复旋律7
后缀自动机四·重复旋律7 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的 ...
随机推荐
- 运行Hadoop start-all.sh遇到的错误ssh: connect to host localhost port 22: Connection refused
ssh: connect to host localhost port 22: Connection refused 我的情况是ssh server没装,查看方法: ps -e |grep ssh 1 ...
- CentOS 7 下 ifconfig command not found 解决办法
1.查看ifconfig命令是否存在 查看 /sbin/ifconfig是否存在 2.如果ifconfig命令存在,查看环境变量设置 [root@localhost ~]# echo $PATH 如果 ...
- How to add a date range picker to filter for dates on a GridView for Yii2 - See more at: http://www.2amigos.us/blog/how-to-add-a-date-range-picker-to-filter-for-dates-on-a-gridview-for-yii2#sthash.pf7
Filtering the data we have on our GridView by dates are sometimes very important. On this article I ...
- Linux 判断进程是否已经运行的程序
bool ServerProcess::isAlreadyRunning() { #ifndef __linux__ WarningLog(<<"can't check if p ...
- 【Linux】Vim编辑器
本文基于Debian 1.vim使用简介 1.1vim安装 使用apt安装vim即可: sudo apt-get install vim 1.2 vim编辑器的模式 vim编辑器分为命令模式和编辑模式 ...
- C,C++面试题2
面试题1:变量的声明和定义有什么区别为变量分配地址和存储空间的称为定义,不分配地址的称为声明.一个变量可以在多个地方声明,但是只在一个地方定义.加入extern修饰的是变量的声明,说明此变量将在文件以 ...
- yum使用,使用rpm指令安装rpm,使用dpkg指令安装deb
yum安装时如果报错提示安装失败,缺少库文件,可以使用: yum whatprovides 库名 之后安装提供的程序 yum remove xxx卸载 yum update 更新 解决 Require ...
- Delphi 实现 图灵机器人API(IDHTTP POST )
此功能所需的 Key及接口文档,都可以在图灵机器人的官网下载, 小伙伴们需要申请自己的图灵机器人账号. 申请方法请自行百度“图灵机器人” . 登录账号后,在左侧的[机器人接入],获取需要的信息,记得 ...
- sampling method
sampling method 背景 在贝叶斯框架下,利用后验分布对参数进行估计,也即 其中 (1)是参数的先验分布. (2)是似然分布,数据集的生成联合概率 (3)是参数的后验分布. 通常分布很复杂 ...
- Wait--查看等待
--清除等待统计 --===================================================== --清除等待统计 DBCC SQLPERF (N'sys.dm_os_ ...