Common Substrings POJ - 3415 (后缀自动机)
Common Substrings
\]
题意
给出两个字符串,要求两个字符串公共子串长度不小于 \(k\) 的对数。
思路
对 \(S\) 串构建后缀自动机,然后利用 \(v \in u'son\),\(dp[u] += dp[v]\) 求出每个节点的 \(endpos\) 大小。
用 \(T\) 串在自动机上跑最长公共连续子串,假设现在在 \(T\) 串上匹配的最长部分是 \(t\),停在自动机上的 \(p\) 节点。为了防止重复计数,我们现在要求就是的 \(t\) 的所有后缀在 \(S\) 上有多少匹配的位置。
这个计算方法就是 \(\sum dp[i]*(LCS-max(k-1,father.len))\)。在 \(p\) 节点时,\(LCS\) 为我们每次更新的答案 \(res\),接下来往 \(p\) 的 \(father\) 更新时,\(LCS\) 就是 \(i.len\)
比如样例中的
\(xx\\
xx\)
第二个串匹配时,第一次匹配到 \(x\)_ 。第二次匹配到 \(xx\),然后我们继续更新 _\(x\) 的答案。
但是如果每次都暴力向上更新,是会超时的,我们发现只有每个刚刚匹配到的 \(p\) 节点的答案与 \(res\) 有关,而 \(p\) 向上更新的节点的贡献都是固定的,所以我们可以先求出全部的 \(p\) 节点的贡献,然后用 \(cnt[i]\) 表示 \(i\) 节点被底下更新了几次,倒着计算,压缩更新次数。
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m, k;
int cas, tol, T;
struct Sam {
struct Node {
int next[55];
int fa, len;
void init() {
mes(next, 0);
fa = len = 0;
}
} node[maxn];
ll dp[maxn], cnt[maxn];
int sz, last;
void init() {
last = sz = 1;
mes(dp, 0);
node[sz].init();
}
void insert(int k) {
int p = last, np = last = ++sz;
dp[np] = 1;
node[np].init();
node[np].len = node[p].len+1;
for(; p&&!node[p].next[k]; p=node[p].fa)
node[p].next[k] = np;
if(p == 0) {
node[np].fa = 1;
} else {
int q = node[p].next[k];
if(node[q].len == node[p].len+1) {
node[np].fa = q;
} else {
int nq = ++sz;
node[nq] = node[q];
node[nq].len = node[p].len+1;
node[np].fa = node[q].fa = nq;
for(; p&&node[p].next[k]==q; p=node[p].fa)
node[p].next[k] = nq;
}
}
}
int tax[maxn], gid[maxn];
void handle() {
for(int i=0; i<=sz; i++) tax[i] = cnt[i] = 0;
for(int i=1; i<=sz; i++) tax[node[i].len]++;
for(int i=1; i<=sz; i++) tax[i] += tax[i-1];
for(int i=1; i<=sz; i++) gid[tax[node[i].len]--] = i;
for(int i=sz; i>=1; i--) {
int u = gid[i];
int fa = node[u].fa;
dp[fa] += dp[u];
}
}
void solve(char *s, int k) {
int len = strlen(s+1);
int p = 1;
ll res = 0, ans = 0;
for(int i=1; i<=len; i++) {
int nst;
if('a'<=s[i] && s[i]<='z') nst = s[i]-'a'+1;
else nst = s[i]-'A'+1+26;
while(p && !node[p].next[nst]) {
p = node[p].fa;
res = node[p].len;
}
if(p == 0) {
p = 1;
res = 0;
} else {
p = node[p].next[nst];
res++;
}
if(res >= k) {
ans += dp[p]*(res - max(node[node[p].fa].len, k-1));
if(node[node[p].fa].len >= k)
cnt[node[p].fa]++;
}
}
for(int i=sz; i>=1; i--) {
int u = gid[i];
ans += dp[u]*cnt[u]*(node[u].len - max(node[node[u].fa].len, k-1));
if(node[node[u].fa].len >= k)
cnt[node[u].fa] += cnt[u];
}
printf("%lld\n", ans);
}
} sam;
char s[maxn], t[maxn];
int main() {
while(scanf("%d", &k), k) {
sam.init();
scanf("%s%s", s+1, t+1);
int slen = strlen(s+1);
for(int i=1; i<=slen; i++) {
int nst;
if('a'<=s[i] && s[i]<='z') nst = s[i]-'a'+1;
else nst = s[i]-'A'+1+26;
sam.insert(nst);
}
sam.handle();
sam.solve(t, k);
}
return 0;
}
Common Substrings POJ - 3415 (后缀自动机)的更多相关文章
- POJ 3415 (后缀自动机)
POJ 3415 Common Substrings Problem : 给两个串S.T (len <= 10^5), 询问两个串有多少个长度大于等于k的子串(位置不同也算). Solution ...
- Common Substrings POJ - 3415(长度不小于k的公共子串的个数)
题意: 给定两个字符串A 和 B, 求长度不小于 k 的公共子串的个数(可以相同) 分两部分求和sa[i-1] > len1 sa[i] < len1 和 sa[i-1] < ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- POJ 3415 后缀数组
题目链接:http://poj.org/problem?id=3415 题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数. 思路:首先是两个字符串的问题.所以想用一个'#'把两个字 ...
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数
Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11469 Accepted: 379 ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- POJ 3518 (后缀自动机)
POJ 3518 Boring Problem : 给一个串S,询问串S有多个子串出现至少两次且位置不重叠. Solution : 对S串建立后缀自动机,再建立后缀树,dfs一遍统计处每个结点的子树中 ...
随机推荐
- Python基础笔记(四)
1. 返回函数与闭包 如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure) def getSum(*args): def add(): ...
- Java可视化计算器
利用java中的AWT和SWING包来做可视化界面. 首先来简单了解一下这两个包: AWT和Swing都是Java中用来做可视化界面的.AWT(Abstract Window Toolkit):抽象窗 ...
- Kafka分布式的消息顺序
Kafka分布式的单位是partition,同一个partition用一个write ahead log组织,所以可以保证FIFO的顺序.不同partition之间不能保证顺序. 但是绝大多数用户都可 ...
- statsvn使用小记
准备工作 1.安装TortoiseSVN,在安装时需要安装svn命令行工具: 2.拉取svn代码: svn co https://svn.myserver.cn/svn/myproject1\trun ...
- 关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能;可能和其他插件有冲突)
关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能:可能和其他插件有冲突)
- python day 22 CSS拾遗之箭头,目录,图标
目录 day 4 learn html 1. CSS拾遗之图标 2. html文件的目录结构 3. CSS拾遗之a包含标签 4. CSS拾遗之箭头画法 day 4 learn html 2019/11 ...
- 半导体行业MES系统应用案例
半导体行业的发展是受惠在集成电路上的,但是收到技术瓶颈的阻碍,所以工业时期对半导体行业就造成了严重的冲击. 为了推动半导体行业快速发展,扭转像IBM.东芝以及富士康等IDM大厂利用晶圆代工对半导体制造 ...
- Java 浮点数的精确度问题
题目:分别获取数字的整数部分.小数部分,如15.12,整数部分为15,小数部分为0.12 package my_package; public class Divide { public static ...
- VirtualDub在处理WMV文件时显示“MISSING CODEC”怎么办
以下内容主要来自:http://www.brilliantcode.com/virtualdub-is-showing-missing-codec-when-i-play-a-wmv-movie-ev ...
- Linux 环境变量配置(Nodejs/MongoDB/JDK/Nginx)
一.环境变量配置 注:配置环境变量的文件 全局变量(系统级别): /etc/bashrc /etc/profile /etc/environment 用户变量(用户级别): ~/.bash_profi ...