POJ 3415 Common Substrings

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

Solution :最开始的想法是将S串和T串先后插入后缀自动机,统计出每个节点对应串的出现次数,不过这种做法被卡空间了。

第二种想法是只将S串插入后缀自动机,建立后缀树,统计出每个节点对应串的出现次数,在统计出每个节点的所有父亲节点的出现次数之和。之后将T串在后缀自动机上进行匹配,假设当前T串在p节点匹配成功,且匹配成功长度为len,那么对答案的贡献就是p节点所有长度超过k的父亲节点,再加上当前节点中长度超过k但不超过

tmp的串。

#include <iostream>
#include <string> using namespace std; const int N = 200008; struct edge
{
int v, nt;
}; struct Suffix_Automanon
{
int nt[N][60], a[N], fail[N];
int tot, last, root;
int lt[N], sum;
int p, q, np, nq;
int cnt[N];
long long f[N];
edge eg[N]; int newnode(int len)
{
for (int i = 0; i < 60; ++i) nt[tot][i] = -1;
fail[tot] = -1; cnt[tot] = f[tot] = lt[tot] = 0; a[tot] = len;
return tot++;
}
void clear()
{
tot = sum = 0;
last = root = newnode(0);
}
void add(int u, int v)
{
eg[++sum] = (edge){v, lt[u]}; lt[u] = sum;
}
void insert(int ch)
{
p = last; np = last = newnode(a[p] + 1); cnt[np] = 1;
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 < 60; ++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)
{
dfs(eg[i].v);
cnt[u] += cnt[eg[i].v];
}
}
void dfs(int u, int k)
{
for (int i = lt[u]; i; i = eg[i].nt)
{
if (u != root && k <= a[u])
{
f[eg[i].v] += f[u] + (a[u] - max(k, a[fail[u]] + 1) + 1) * cnt[u];
}
dfs(eg[i].v, k);
}
}
void build(int k)
{
for (int i = 1; i < tot; ++i) add(fail[i], i);
dfs(root);
dfs(root, k);
}
void solve(const string &s, int k)
{
int p = root, tmp = 0;
long long ans = 0;
for (int i = 0, len = s.length(); i < len; ++i)
{
int ch = s[i] - 'A';
if (~nt[p][ch]) p = nt[p][ch], tmp++;
else
{
for (; ~p && nt[p][ch] == -1; p = fail[p]);
if (p == -1) p = root, tmp = 0;
else
{
tmp = a[p] + 1;
p = nt[p][ch];
}
}
if (p != root)
{
ans += f[p];
if (tmp >= k && k <= a[p]) ans += (min(a[p], tmp) - max(k, a[fail[p]] + 1) + 1) * cnt[p];
}
}
cout << ans << endl;
} }sam; int main()
{
int n; string s, t;
while (cin >> n >> s >> t)
{
sam.clear();
for (int i = 0, len = s.length(); i < len; ++i)
sam.insert(s[i] - 'A');
sam.build(n);
sam.solve(t, n);
}
}

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

  1. POJ 3415 后缀数组

    题目链接:http://poj.org/problem?id=3415 题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数. 思路:首先是两个字符串的问题.所以想用一个'#'把两个字 ...

  2. POJ 3518 (后缀自动机)

    POJ 3518 Boring Problem : 给一个串S,询问串S有多个子串出现至少两次且位置不重叠. Solution : 对S串建立后缀自动机,再建立后缀树,dfs一遍统计处每个结点的子树中 ...

  3. POJ - 2774~POJ - 3415 后缀数组求解公共字串问题

    POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...

  4. poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数

    Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 379 ...

  5. POJ - 1743 后缀自动机

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

  6. POJ 3415 后缀数组+单调栈

    题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...

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

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

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

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

  9. POJ 3415 Common Substrings(后缀数组 + 单调栈)题解

    题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...

随机推荐

  1. D. Leaving Auction 一题很好的思维题

    http://codeforces.com/contest/749/problem/D 现在发现做题要把样例抄下来,然后画一画,这样才容易发现新大陆.嗯,以后做题就这样. 如果排除了被删除了的人,那么 ...

  2. jQuery幸运大转盘_jQuery+PHP抽奖程序

    http://www.thinkphp.cn/code/1153.html 网上转盘抽奖程序大多是flash完成的,而本文使用jQuery和PHP来实现转盘抽奖程序. 若是想看更多js特效.网站源码. ...

  3. java之java.sql.SQLException: ResultSet is from UPDATE. No Data.

    问题解释:java调用存储过程的时候,查询结果不能通过ResultSet来查询,需要通过CallableStatement来查询, 比如: ResultSet rs = callableStateme ...

  4. vuetifyjs简介及其使用

    何为 vuetify 一个vue ui库,提供vue组件供使用.根据 Google Material Design 指南实现(https://material.io/).Vuetify支持SSR(服务 ...

  5. (一)Redis for Windows正确打开方式

    目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis 下载地址 官网.中文网1 及 中 ...

  6. 微信小程序中的图形验证码

    可以在utils中新建一个mcaptcha.js 代码如下: module.exports = class Mcaptcha { constructor(options) { this.options ...

  7. python多个装饰器的执行顺序

    def decorator_a(func): print 'Get in decorator_a' def inner_a(*args, **kwargs): print 'Get in inner_ ...

  8. day25-1 网络架构与互联网组成

    目录 网络架构 单机架构 CS架构 数据放在服务端和客户端的利与弊 BS架构 互联网和互联网的组成 互联网的硬件组成 互联网的软件组成 网络架构 单机架构 应用领域: 单机游戏 CS架构 基于网络,应 ...

  9. day24-1 元类

    目录 元类 类的组成 内置函数 exec() class关键字创建类原理 自定义元类控制类的创建 自定义元类控制类实例化 自定义元类后对象属性查找顺序 元类 在python中一切皆对象,name我们用 ...

  10. Python框架Django的入门

    本篇文章主要给大家介绍Django的入门知识: