POJ 3415 Common Substrings ——后缀数组
【题目分析】
判断有多少个长度不小于k的相同子串的数目。
N^2显然是可以做到的。
其实可以维护一个关于height的单调栈,统计一下贡献,就可以了。
其实还是挺难写的OTZ。
【代码】
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 300005
#define LL long long
#define inf 0x3f3f3f3f
#define F(i,j,k) for (LL i=j;i<=k;++i)
#define D(i,j,k) for (LL i=j;i>=k;--i)
void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("wa.txt","w",stdout);
#endif
}
LL Getint()
{
LL x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
char ss[maxn];
LL n,l1,l2,k;
struct SuffixArray{
LL s[maxn];
LL rk[maxn],h[maxn],cnt[maxn],tmp[maxn],sa[maxn];
void init()
{
memset(s,0,sizeof s);
// memset(rk,0,sizeof rk);
// memset(h,0,sizeof h);
// memset(cnt,0,sizeof cnt);
// memset(tmp,0,sizeof tmp);
// memset(sa,0,sizeof sa);
}
void build(LL n,LL m)
{
LL i,j,k; n++;
F(i,0,2*n+5) rk[i]=h[i]=tmp[i]=sa[i]=0;
F(i,0,m-1) cnt[i]=0;
F(i,0,n-1) cnt[rk[i]=s[i]]++;
F(i,1,m-1) cnt[i]+=cnt[i-1];
F(i,0,n-1) sa[--cnt[rk[i]]]=i;
for (k=1;k<=n;k<<=1)
{
F(i,0,n-1)
{
j=sa[i]-k;
if (j<0) j+=n;
tmp[cnt[rk[j]]++]=j;
}
sa[tmp[cnt[0]=0]]=j=0;
F(i,1,n-1)
{
if (rk[tmp[i]]!=rk[tmp[i-1]]||rk[tmp[i]+k]!=rk[tmp[i-1]+k]) cnt[++j]=i;
sa[tmp[i]]=j;
}
memcpy(rk,sa,n*sizeof(LL));
memcpy(sa,tmp,n*sizeof(LL));
if (j>=n-1) break;
}
for (j=rk[h[i=k=0]=0];i<n-1;++i,++k)
while (~k&&s[i]!=s[sa[j-1]+k]) h[j]=k--,j=rk[sa[j]+1];
//Debug
/*
F(i,0,n-1) cout<<s[i]<<" "; cout<<endl;
F(i,0,n-1) cout<<sa[i]<<" ";cout<<endl;
F(i,0,n-1) cout<<h[i]<<" ";cout<<endl;
*/
//Debug over
}
LL sta[maxn][2],top;
LL tot,sum;
void solve(LL n,LL k)
{
// n++;
top=0;sum=0;tot=0;
F(i,1,n)
{
if (h[i]<k) top=tot=0;
else
{
LL cnt=0;
if (sa[i-1]<l1) cnt++,tot+=h[i]-k+1;
while (top>0&&h[i]<=sta[top-1][0])
{
top--;
tot-=sta[top][1]*(sta[top][0]-h[i]);
cnt+=sta[top][1];
}
sta[top][0]=h[i]; sta[top++][1]=cnt;
if (sa[i]>l1) sum+=tot;
}
}
top=tot=0;
F(i,1,n)
{
if (h[i]<k) top=tot=0;
else
{
LL cnt=0;
if (sa[i-1]>l1) cnt++,tot+=h[i]-k+1;
while (top>0&&h[i]<=sta[top-1][0])
{
top--;
tot-=sta[top][1]*(sta[top][0]-h[i]);
cnt+=sta[top][1];
}
sta[top][0]=h[i]; sta[top++][1]=cnt;
if (sa[i]<l1) sum+=tot;
}
}
cout<<sum<<endl;
}
}arr;
int main()
{
Finout();
while (scanf("%lld",&k)!=EOF&&k)
{
arr.init();
memset(ss,0,sizeof ss);
scanf("%s",ss);l1=strlen(ss);//cout<<l1<<endl;
F(i,0,l1-1) arr.s[i]=ss[i];
arr.s[l1]=248;
memset(ss,0,sizeof ss);
scanf("%s",ss);l2=strlen(ss);//cout<<l2<<endl;
F(i,0,l2-1) arr.s[l1+i+1]=ss[i];
arr.build(l1+l2+1,250);
arr.solve(l1+l2+1,k);
}
}
POJ 3415 Common Substrings ——后缀数组的更多相关文章
- poj 3415 Common Substrings —— 后缀数组+单调栈
题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...
- poj 3415 Common Substrings——后缀数组+单调栈
题目:http://poj.org/problem?id=3415 因为求 LCP 是后缀数组的 ht[ ] 上的一段取 min ,所以考虑算出 ht[ ] 之后枚举每个位置作为右端的贡献. 一开始想 ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- 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 后缀数组+单调栈
题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...
- poj 3415 Common Substrings - 后缀数组 - 二分答案 - 单调栈
题目传送门 传送点I 传送点II 题目大意 给定串$A, B$,求$A$和$B$长度大于等于$k$的公共子串的数量. 根据常用套路,用一个奇怪的字符把$A$,$B$连接起来,然后二分答案,然后按mid ...
- POJ - 3415 Common Substrings(后缀数组求长度不小于 k 的公共子串的个数+单调栈优化)
Description A substring of a string T is defined as: T( i, k)= TiTi+1... Ti+k-1, 1≤ i≤ i+k-1≤| T|. G ...
- POJ.3145.Common Substrings(后缀数组 倍增 单调栈)
题目链接 \(Description\) 求两个字符串长度不小于k的公共子串对数. \(Solution\) 求出ht[]后先减去k,这样对于两个后缀A',B',它们之间的贡献为min{ht(A)}( ...
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
随机推荐
- HTML5 —— 自学第一课
1.心得 首先遇见问题要寻根源,而不是将问题抛出:其次要经常查看参考文档.参考示例:学会百度. 2.技能需求 HTML5.XHTML.CSS3.JavaScript.jQuery(jQuery-UI/ ...
- java发送 email
public class EmailUtils implements IAction { private static Logger logger = Logger.getLogger(EmailUt ...
- Postgresql 简单配置 (ubuntu server 14.04.3)
安装和配置 ubuntu server 已经自动安装了progresql,故安装步骤就省略 初始postgresql没有密码,不能使用,需要先设置密码,命令(从网上随意找的)如下: sudo su p ...
- webservice客户端开发
一.根据约定条件生成客户端 1.需要axis.jar包 2.调用 import org.apache.axis.client.Call; import org.apache.axis.client.S ...
- AVPlayer
AVPlayer AVPlayerLayer是CALayer的一个子类,由于AVPlayer这个播放器只能安置在AVPlayerLayer 这个图层之上,所以我们需要实例化一个UIView,并 ...
- C#操作XML总结
1.using System.Xml; using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xm ...
- Uiautomator--Uiselector元素定位
一.UiSelector作用 按照一定的条件(例如控件的text值,资源id),定位界面上的元素.UiSelector对象的最终目的是去构造一个UiObject对象. 二.元素定位 1.根据text定 ...
- Python: Win7 64位如何安装MongoDB?
Windows 7 64位安装MongoDB 官网tutorial: https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-window ...
- Windows 网络编程
网络编程 API ,失败返回 -,错误代码 WSASYSNOTREADY 表示基础网络子系统没有准备好网络通行,WSAVERNOTSUPPORTED 表示 Socket 版本不支持,WSAEINPRO ...
- ionic + cordova+angularJs 搭建的H5 App完整版总结
为期半个月的项目实践开发,已完整告一段落,团队小组获得第一名,辛苦总算没有白费,想起有一天晚上,整个小组的人,联调到12点才从公司回去,真是心酸.这里总结一下,项目过程中遇到的问题 和感悟.哈哈, ...