POJ 3518 Boring

Problem : 给一个串S,询问串S有多个子串出现至少两次且位置不重叠。

Solution : 对S串建立后缀自动机,再建立后缀树,dfs一遍统计处每个结点的子树中最长节点max和最短节点min。枚举一遍后缀自动机的节点,那么对于其对应后缀的长度要求为小于等于max - min。

#include <iostream>
#include <algorithm> using namespace std; const int N = 1000008;
const int INF = 2000000008; struct edge
{
int u, v, nt;
}; struct suffix_automanon
{
int nt[N][26], fail[N], a[N], qmin[N], qmax[N];
int tot, last, root;
int lt[N], sum;
int p, q, np, nq;
edge eg[N << 1];
void add(int u, int v)
{
eg[++sum] = (edge){u, v, lt[u]}; lt[u] = sum;
}
int newnode(int len)
{
for (int i = 0; i < 26; ++i) nt[tot][i] = -1;
fail[tot] = -1; a[tot] = len; qmax[tot] = 0; qmin[tot] = INF;
lt[tot] = 0;
return tot++;
}
void clear()
{
tot = 0;
root = last = newnode(0);
}
void insert(int ch)
{
p = last; np = last = newnode(a[p] + 1); qmin[np] = qmax[np] = a[np];
for (; ~p && nt[p][ch] == -1; p = fail[p]) nt[p][ch] = np;
if (p == -1) fail[np] = root;
else
{
q = nt[p][ch];
if (a[p] + 1 == a[q]) fail[np] = q;
else
{
nq = newnode(a[p] + 1);
for (int i = 0; i < 26; ++i) nt[nq][i] = nt[q][i];
fail[nq] = fail[q];
fail[q] = fail[np] = nq;
for (; ~p && nt[p][ch] == q; p = fail[p]) nt[p][ch] = nq;
}
}
}
void dfs(int u)
{
for (int i = lt[u]; i; i = eg[i].nt)
{
// cout << u << " " << eg[i].v << endl;
int v = eg[i].v;
dfs(v);
qmax[u] = max(qmax[u], qmax[v]);
qmin[u] = min(qmin[u], qmin[v]);
}
}
void solve()
{
long long ans = 0;
for (int i = 1; i < tot; ++i) add(fail[i], i);
dfs(root);
// for (int i = 1; i < tot; ++i) cout << qmin[i] << " " << qmax[i] << endl;
for (int i = 1; i < tot; ++i)
{
int len = qmax[i] - qmin[i];
if (len > a[fail[i]]) ans += min(a[i], len) - a[fail[i]];
}
cout << ans << endl;
} }sam; int main()
{
string s;
while (cin >> s)
{
if (s == "#") break;
sam.clear();
for (int i = 0, len = s.length(); i < len; ++i)
sam.insert(s[i] - 'a');
sam.solve();
}
}

POJ 3518 (后缀自动机)的更多相关文章

  1. POJ 3415 (后缀自动机)

    POJ 3415 Common Substrings Problem : 给两个串S.T (len <= 10^5), 询问两个串有多少个长度大于等于k的子串(位置不同也算). Solution ...

  2. Boring counting HDU - 3518 后缀自动机

    题意: 对于给出的字符串S, 长度不超过1000, 求其中本质不同的子串的数量, 这些子串满足在字符串S中出现了至少不重合的2次 题解: 将串放入后缀自动机中然后求出每一个节点对应的子串为后缀的子串出 ...

  3. POJ - 1743 后缀自动机

    POJ - 1743 顺着原字符串找到所有叶子节点,然后自下而上更新,每个节点right的最左和最右,然后求出答案. #include<cstdio> #include<cstrin ...

  4. POJ 1509 Glass Beads 后缀自动机 模板 字符串的最小表示

    http://poj.org/problem?id=1509 后缀自动机其实就是一个压缩储存空间时间(对节点重复利用)的储存所有一个字符串所有子串的trie树,如果想不起来长什么样子可以百度一下找个图 ...

  5. UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)

    题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...

  6. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

  7. poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树

    题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...

  8. Common Substrings POJ - 3415 (后缀自动机)

    Common Substrings \[ Time Limit: 5000 ms\quad Memory Limit: 65536 kB \] 题意 给出两个字符串,要求两个字符串公共子串长度不小于 ...

  9. POJ - 2774 Long Long Message (后缀数组/后缀自动机模板题)

    后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> ...

随机推荐

  1. 451 Sort Characters By Frequency 根据字符出现频率排序

    给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...

  2. 1043 幸运号码 数位DP

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 设dp[i][j]表示前i位数中,i位数的和为j时的所有情况. 转 ...

  3. 多功能Markdown编辑器MarkdownPad 2的下载、安装和初步使用步骤(图文详解)(博主推荐)

    不多说,直接上干货!   MarkdownPad 是什么? 一.MarkdownPad 2的下载 http://markdownpad.com/download/markdownpad2-setup. ...

  4. Codeforces Beta Round #98 (Div. 2)(A-E)

    A #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  5. 纯CSS写的对勾样式

    & .cicle{          position: relative;          float: right;          margin-right: -1rem;      ...

  6. GatewayWorker+Laravel demo

    GatewayWorker 结合 Laravel 使用的简单案例,重点是在Laravel中使用GatewayClient发送消息 主要流程:GatewayWorker主要负责推送消息给客户端但不接受客 ...

  7. Dragger2解析(一)

    依赖注入(DI-Dependency Injection) 什么是依赖注入 这是一种设计思想,一个面向对象的编程法则. DI能够让开发者写出低耦合代码,更加优良的程序. 更容易测试,代码健壮性更强. ...

  8. 微信小程序组件解读和分析:七、button按钮

    button按钮组件说明: button,顾名思义,按钮,类似于html的button标签.我们可以设置按钮的属性,比如字体颜色大小,背景颜色等,可以给按钮绑定事件,用户点击时会触发事件. butto ...

  9. Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图的数据操作

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图实体类映射 下一篇:Farseer.net轻量级ORM开源 ...

  10. PHP面向对象考察点

    面向对象三大特性 封装 封装性就是把对象的属性和服务结合成一个独立的相同单位,并尽可能隐蔽对象的内部细节,包含两个含义: 把对象的全部属性和全部服务结合在一起,形成一个不可分割的独立单位(即对象). ...