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. 一个简单的公式——求小于N且与N互质的数的和

    首先看一个简单的东西. 若$gcd(i,n)=1$,则有$gcd(n-i,n)=1$ 于是在小于$n$且与$n$互质的数中,$i$与$n-i$总是成对存在,且相加等于$n$. 考虑$i=n-i$的特殊 ...

  2. .NET 微信开发之 获取用户数据

    通过微信接口获取用户信息主要分为以下几个步骤: a.获取公众号的access_token b.通过查询所有用户OPenid接口获取所有用户. string url = "https://ap ...

  3. 掌握Spark机器学习库-08.7-决策树算法实现分类

    数据集 iris.data 数据集概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.Spark ...

  4. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  5. 解决VS2010提示warning C4068: 未知的杂注

    出现原因是#pragma声明问题,加上#pragma warning(disable:4068)即可 #pragma warning(disable:4068)#pragma execution_ch ...

  6. PPTP的搭建

    一.准备 1.检查是否支持pptp modprobe ppp-compress-18 && echo yes yes支持 2.是否开启tun cat /dev/net/tun 返回ca ...

  7. codeforces_B. Forgery

    http://codeforces.com/contest/1059/problem/B 题意: For simplicity, the signature is represented as an  ...

  8. Python之__class__.__module__,__class__.__name__

  9. Learning Discriminative and Transformation Covariant Local Feature Detectors实验环境搭建详细过程

    依赖项: Python 3.4.3 tensorflow>1.0.0, tqdm, cv2, exifread, skimage, glob 1.安装tensorflow:https://www ...

  10. intellij idea集成github

    IDEA配置github并上传项目 http://www.cnblogs.com/jinjiyese153/p/6796668.html github ssl验证 https://www.cnblog ...