hdu_1403_Longest Common Substring(后缀数组的应用)
题目链接:hdu_1403_Longest Common Substring
题意:
给你两个字符串,然你找最长的公共子串
题解:
后缀数组的经典应用,要找两个字符串的公共子串,那么就相当于找两个串的后缀的最长公共前缀,我们将两个字符串拼接在一起,中间插一个特殊字符
然后我们考虑height数组,height数组存的是排i和i-1的最长前缀,如果sa[i]和sa[i-1]在特殊字符的两边,那么这个height[i]记录的就是这两个串的最长
子串,然后扫一遍height数组更新一下答案就行了
- #include<bits/stdc++.h>
- #define F(i,a,b) for(int i=a;i<=b;++i)
- using namespace std;
- namespace suffixarray{
- #define FN(n) for(int i=0;i<n;i++)
- const int N =2E5+;//字符串长度
- int rnk[N],sa[N],height[N],c[N];char s[N];
- void getsa(int n,int m,int *x=rnk,int *y=height){
- FN(m)c[i]=;FN(n)c[x[i]=s[i]]++;FN(m)c[i+]+=c[i];
- for(int i=n-;i>=;i--)sa[--c[x[i]]]=i;
- for(int k=,p;p=,k<=n;k=p>=n?N:k<<,m=p){
- for(int i=n-k;i<n;i++)y[p++]=i;
- FN(n)if(sa[i]>=k)y[p++]=sa[i]-k;
- FN(m)c[i]=;FN(n)c[x[y[i]]]++;FN(m)c[i+]+=c[i];
- for(int i=n-;i>=;i--)sa[--c[x[y[i]]]]=y[i];
- swap(x,y),p=,x[sa[]]=;
- for(int i=;i<n;i++)
- x[sa[i]]=y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k]?p-:p++;
- }
- FN(n)rnk[sa[i]]=i;
- for(int i=,j,k=;i<n-;height[rnk[i++]]=k)
- for(k=k?k-:k,j=sa[rnk[i]-];s[i+k]==s[j+k];k++);
- }
- }
- using namespace suffixarray;
- int main(){
- while(~scanf("%s",s))
- {
- int len=strlen(s),sz;
- s[len]='$';
- scanf("%s",s+len+);
- sz=strlen(s),getsa(sz+,);
- int ans=;
- F(i,,sz)if(ans<height[i]&&(sa[i]-len)*(sa[i-]-len)<)ans=height[i];
- printf("%d\n",ans);
- }
- return ;
- }
hdu_1403_Longest Common Substring(后缀数组的应用)的更多相关文章
- hdu 1403 Longest Common Substring 后缀数组 模板题
题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- HDU 5769 Substring 后缀数组
Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- 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 ...
- POJ3693 Maximum repetition substring 后缀数组
POJ - 3693 Maximum repetition substring 题意 输入一个串,求重复次数最多的连续重复字串,如果有次数相同的,则输出字典序最小的 Sample input ccab ...
- 2016多校联合训练4 F - Substring 后缀数组
Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...
随机推荐
- Voting
Voting time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- 换行符在ajax中返回json,eval时发生的 Unexpected token ILLEGAL
用户如果输入了换行在数据中记录为‘空格’,但不是真正的空格. 程序前台采用ajax和json返回数据绑定时会 出现 Unexpected token ILLEGAL 例子: 在sql中存储为下图 在“ ...
- python+selenium+Eclipse安装
1.安装python 参考安装python:http://www.cnblogs.com/beyongblue/p/4215740.html 2.安装python管理工具setuptools 3.安装 ...
- Application.DoEvents()的作用
记得第一次使用Application.DoEvents()是为了在加载大量数据时能够有一个数据加载的提示,不至于系统出现假死的现象,当时也没有深入的去研究他的原理是怎样的,结果在很多地方都用上了App ...
- css3 div垂直居中
css3: #dd{ height: 300px; background: #0000cc; display: -webkit-box; display: flex; display: -webkit ...
- hdu1021
#include <stdio.h> int fib(int m){ int n_2=1,n_1=2,n,i; if(m==0)return 1; if(m==1)return 2; fo ...
- 一键批量ping任意ip段的存活主机
=======================by me===================================== @echo offecho.color FC for /f %%i ...
- POJ 2234 Matches Game(取火柴博弈1)
传送门 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- P2P直连?经服务器中转?
当同一个系统的两个客户端A.B相互发送消息给对方时,如果它们之间存在P2P通道,那么消息传送的路径就有两种:直接经P2P通道传送.或者经服务器中转.如下图所示: 通常就一般应用而言,如果P2P通道能够 ...
- Python & virtualenv使用说明
virtualenv是virtual environment的缩写,可以创建独立的Python环境,用起来比较干净: 安装(已安装pip 或者 easy_install): 如果使用pip: pi ...