字符串(后缀数组):POJ 3415 Common Substrings
Description
A substring of a string T is defined as:
T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|.
Given two strings A, B and one integer K, we define S, a set of triples (i, j, k):
S = {(i, j, k) | k≥K, A(i, k)=B(j, k)}.
You are to give the value of |S| for specific A, B and K.
Input
The input file contains several blocks of data. For each block, the first line contains one integer K, followed by two lines containing strings A and B, respectively. The input file is ended by K=0.
1 ≤ |A|, |B| ≤ 105
1 ≤ K ≤ min{|A|, |B|}
Characters of A and B are all Latin letters.
Output
For each case, output an integer |S|.
Sample Input
2
aababaa
abaabaa
1
xx
xx
0
Sample Output
22
5
这道题呃,有些考验程序实践能力。
题意:对于给定的两个字符串和一个整数K,求两个字符串长度大于等于K的公共子串数目。
将两个字符串接起来,中间用一个特殊字符隔开,枚举Lcp,暴力枚举是O(n³)的,死活都不可能过。
这是我们想:能否使用以前枚举的信息?所以正解就出来了:单调栈优化!
具体咋打就看代码吧~~~
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
char S[maxn];
int sa[maxn],r[maxn],rank[maxn],lcp[maxn];
int Wv[maxn],Ws[maxn],Wa[maxn],Wb[maxn],len; bool cmp(int *p,int a,int b,int l){
return p[a]==p[b]&&p[a+l]==p[b+l];
} void DA(int n,int m){
int i,j,p,*x=Wa,*y=Wb,*t;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[x[i]=r[i]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)sa[--Ws[x[i]]]=i; for(j=,p=;p<n;m=p,j<<=){
for(p=,i=n-j;i<n;i++)y[p++]=i;
for(i=;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j; for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[Wv[i]=x[y[i]]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)
sa[--Ws[Wv[i]]]=y[i]; for(t=x,x=y,y=t,i=,p=,x[sa[]]=;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
} void Lcp(int n){
int i,j,k=;
for(i=;i<=n;i++)rank[sa[i]]=i;
for(i=;i<n;lcp[rank[i++]]=k)
for(k?--k:k,j=sa[rank[i]-];r[i+k]==r[j+k];++k);
} int s[maxn][]; int main(){
int n,k;
while(~scanf("%d",&k)&&k){
scanf("%s",S);
n=strlen(S);S[n]='%';
scanf("%s",S+n+);
len=strlen(S);
for(int i=;i<len;i++)
r[i]=S[i];
r[len]=;
DA(len+,);
Lcp(len); int cnt=;
long long ans=,sum=;
for(int i=;i<=len;i++){
if(lcp[i]<k){
sum=;cnt=;
continue;
}
int tot=;
if(sa[i-]>n){
sum+=lcp[i]-k+;
tot++;
}
while(cnt&&s[cnt][]>=lcp[i]){
tot+=s[cnt][];
sum-=1ll*s[cnt][]*(s[cnt][]-lcp[i]);
cnt--;
}
s[++cnt][]=lcp[i];
s[cnt][]=tot;
if(sa[i]<n)ans+=sum;
}
cnt=;sum=;
for(int i=;i<=len;i++){
if(lcp[i]<k){
sum=;cnt=;
continue;
}
int tot=;
if(sa[i-]<n){
sum+=lcp[i]-k+;
tot++;
}
while(cnt&&s[cnt][]>=lcp[i]){
tot+=s[cnt][];
sum-=1ll*s[cnt][]*(s[cnt][]-lcp[i]);
cnt--;
}
s[++cnt][]=lcp[i];
s[cnt][]=tot;
if(sa[i]>n)ans+=sum;
}
printf("%lld\n",ans);
}
return ;
}
字符串(后缀数组):POJ 3415 Common Substrings的更多相关文章
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- poj 3415 Common Substrings——后缀数组+单调栈
题目:http://poj.org/problem?id=3415 因为求 LCP 是后缀数组的 ht[ ] 上的一段取 min ,所以考虑算出 ht[ ] 之后枚举每个位置作为右端的贡献. 一开始想 ...
- poj 3415 Common Substrings —— 后缀数组+单调栈
题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...
- POJ 3415 Common Substrings(后缀数组)
Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. Given t ...
- POJ - 3415 Common Substrings (后缀数组)
A substring of a string T is defined as: T( i, k)= TiTi +1... Ti+k -1, 1≤ i≤ i+k-1≤| T|. Given two s ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- poj 3415 Common Substrings
题目链接:http://poj.org/problem?id=3415 题目分类:后缀数组 题意:给出两个串和一个数字k,求两个串的公共字串大于等于k的数目 代码: //#include<bit ...
- POJ 3415 Common Substrings 【长度不小于 K 的公共子串的个数】
传送门:http://poj.org/problem?id=3415 题意:给定两个串,求长度不小于 k 的公共子串的个数 解题思路: 常用技巧,通过在中间添加特殊标记符连接两个串,把两个串的问题转换 ...
随机推荐
- tj
--统计set @collSql='select sum(case Ca_IssueType when 0 then 1 else 0 end) as IssueCount,sum(case when ...
- Java-Android 之输入提示框
Android的文本提示框有两种方式: main.xml文件 <?xml version="1.0" encoding="utf-8"?> < ...
- 关键词:CodeSmith工具、Money类型、__UNKNOWN__
问题描述: 当数据库列类型有Money类型的时候,CodeSmith生成数据访问层会出错.有不能识别的类型.解决方法: 通过查找资料得知,数据库中的Money类型在DbType中是Currency(货 ...
- R文件丢失异常原因汇总
引言: R文件丢失异常在java开发中是个比较常见的异常,造成这个异常的原因可能非常微小,但是给Android开发者们造成的麻烦可是巨大的,当程序员们费尽千辛万苦,找到自己错在哪里的时候,绝对会对自己 ...
- MSSQL批量替换网址字符串语句
1.如何批量替换ntext字段里面的数据 问题描述: 我想把数据库中News表中的字段content中的一些字符批量替换. 我的content字段是ntext类型的. 我想替换的字段是content字 ...
- 更新xcode后插件失效问题——不针对特定版本的通用解决方法
一.Xcode更新后插件失效的原理 1.每次更新Xcode后插件都会失效,其实插件都还在这个目录好好的躺着呢: ~/Library/Application Support/Developer/Shar ...
- [转]PHP取整函数:ceil,floor,round,intval的区别详细解析
我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. 1.ceil -- 进一法取整 说明float ceil ( float value ) 返回不小于 value ...
- SpringMVC4+thymeleaf3的一个简单实例(篇五:页面和MySql的数据交互-展示以及存储)
这一篇将介绍怎样把页面数据保存的MySQL数据库,并将数据库内容展示到页面上.首先做一个基础工作,添加以下jar到lib:1: mysql-connector-Java-5.1.40-bin.jar ...
- SpringMVC4+thymeleaf3的一个简单实例(篇二:springMVC与thymeleaf的整合)
延续前篇内容. 开始之前,我们首先要准备以下12个jar文件:spring-aop-4.3.3.RELEASE.jarspring-beans-4.3.3.RELEASE.jarspring-cont ...
- npm不能安装任何包,报错:npm WARN onload-script failed to require onload script npm-autoinit/autoinit及解决方法
想要利用Hexo搭建一个博客,但是安装时npm一直报错,不仅仅是Hexo包,连别的其他包也不行,会提示下面的一堆错误 npm WARN onload-script failed to require ...