Ural1517 所谓后缀数组, 实际上准确的说,应该是排序后缀数组。

一个长度为N的字符串,显然有N个后缀,将他们放入一个数组中并按字典序排序就是后缀数组的任务。

这个数组有很好的性质,使得我们运行一些算法时 可以大幅度的优化。

本题就是后缀数组的一个主要应用, 快速求得后缀S(i)和S(j)的最长公共前缀LCP。

**由字典序的性质可知 S(i)和S(j)的LCP长度就是 h[sa[i]+1],h[sa[i]+2].... h[sa[j]]中的最小值,证明显然。

**而如何计算h数组呢? 有一个性质 h[rank[i]]>=h[rank[i-1]]-1. 由两两对应关系可以证明(同时去掉首字符)。

代码如下 后缀数组并不复杂

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue> using namespace std;
const int MAXN=200000+100;
void radix(int *str,int *a,int *b,int n,int m)
{
static int count[MAXN];
memset(count,0,sizeof(count));
for(int i=0;i<n;++i)++count[str[a[i]]];
for(int i=1;i<=m;++i)count[i]+=count[i-1];
for(int i=n-1;i>=0;--i)b[--count[str[a[i]]]]=a[i];
} void sorted_suffix_array(int *str,int *sa,int n,int m)
{
static int rank[MAXN],a[MAXN],b[MAXN];
for(int i=0;i<n;++i)rank[i]=i;
radix(str,rank,sa,n,m); rank[sa[0]]=0;
for(int i=1;i<n;++i)rank[sa[i]]=rank[sa[i-1]]+(str[sa[i]]!=str[sa[i-1]]);
for(int i=0;(1<<i) <n;++i)
{
for(int j=0;j<n;++j)
{
a[j]=rank[j]+1;
b[j]=j+(1<<i)>=n? 0:rank[j+(1<<i)]+1;
sa[j]=j;
}
radix(b,sa,rank,n,n);
radix(a,rank,sa,n,n);
rank[sa[0]]=0;
for(int j=1;j<n;++j)
{
rank[sa[j]]=rank[sa[j-1]]+(a[sa[j-1]]!=a[sa[j]]||b[sa[j-1]]!=b[sa[j]]);
}
}
} void calc_height(int *str,int *sa,int *h,int n)
{
static int Rank[MAXN];
int k=0;
h[0]=0;
for(int i=0;i<n;++i)Rank[sa[i]]=i;
for(int i=0;i<n;++i)
{
k= k==0?0:k-1;
if(Rank[i]!=0)
while(str[i+k]==str[sa[Rank[i]-1]+k])++k;
h[Rank[i]]=k;
}
} int work(string a,string b)
{
static int s[MAXN],sa[MAXN],h[MAXN];
string str;
str=a+"#"+b;
copy(str.begin(),str.end(),s);
sorted_suffix_array(s,sa,str.length(),str.length()+256);
calc_height(s,sa,h,str.length());
int ans=0,pos;
for(int i=1;i<str.length();++i)
{
if(h[i]>ans&&(sa[i-1]<a.length())!=(sa[i]<a.length()))
{
ans=h[i];
pos=sa[i];
}
}
cout<<str.substr(pos,ans)<<endl;
} int main()
{freopen("t.txt","r",stdin);
ios::sync_with_stdio(false);
int n;
cin>>n;
string a,b;
cin>>a>>b;
work(a,b);
return 0;
}

  

Ural 1517. Freedom of Choice 后缀数组的更多相关文章

  1. URAL 1517 Freedom of Choice(后缀数组,最长公共字串)

    题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...

  2. URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/whyorwhnt/article/details/34075603 题意:给出两个串的长度(一样长) ...

  3. URAL 1517 Freedom of Choice

    Freedom of Choice Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on Ural. Orig ...

  4. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...

  5. URAL 1297 最长回文子串(后缀数组)

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  6. 后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

    1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 MB The “U.S. Robots” HQ has just received a ...

  7. ural 1297(后缀数组+RMQ)

    题意:就是让你求一个字符串中的最长回文,如果有多个长度相等的最长回文,那就输出第一个最长回文. 思路:这是后缀数组的一种常见的应用,首先把原始字符串倒转过来,然后接在原始字符串的后面,中间用一个不可能 ...

  8. URAL 1297 Palindrome(后缀数组+ST表)

    [题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...

  9. URAL 1297 后缀数组:求最长回文子串

    思路:这题下午搞了然后一直WA,后面就看了Discuss,里面有个数组:ABCDEFDCBA,这个我输出ABCD,所以错了. 然后才知道自己写的后缀数组对这个回文子串有bug,然后就不知道怎么改了. ...

随机推荐

  1. php7 安装swoole扩展

    昨天无意中看到一篇关于直播的视频教程 里面讲到了swoole,对于这个东西我相信大家(接近1年phper)都是听过它,但没有真正去用它,当然也是不知道如何使用(me too). 此处总结一下(借鉴了几 ...

  2. POJ-1028Web Navigation,大水题坑我3遍

    Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32963   Accepted: 14704 ...

  3. C++字符串读入

    int read() { ,f=;char ch=getchar(); ;ch=getchar();} +ch-';ch=getchar();} return x*f; } int main() { ...

  4. 【BZOJ4559】成绩比较(组合计数,容斥原理)

    题意: G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M- 1的整数.一位同学在必修课上可以获得的分数是1到Ui中的一个整数.如果在每门 ...

  5. openjudge1944 吃糖果

    描述名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N >0).妈妈告诉名名每天可以吃一块或者两块巧克力.假设名名每天都吃巧克力,问名名共有多 ...

  6. 【IntelliJ 】IntelliJ IDEA 自动导入包 快捷方式 关闭重复代码提示

    idea可以自动优化导入包,但是有多个同名的类调用不同的包,必须自己手动Alt+Enter设置 设置idea导入包 勾选标注 1 选项,IntelliJ IDEA 将在我们书写代码的时候自动帮我们优化 ...

  7. Toast自定义

    Toast toast=new Toast(MainActivity.this); toast.setView(getLayoutInflater().inflate(R.layout.toast,n ...

  8. Spring Boot - how to configure port

    https://stackoverflow.com/questions/21083170/spring-boot-how-to-configure-port

  9. 绿盟NF防火墙系统

    http://www.nsfocus.com.cn/ http://www.nsfocus.com.cn/products/details_22_5.html

  10. Mybatis中的ognl表达式。及myabtis where标签/if test标签/trim标签

    1.mybatis默认支持使用ognl表达式来生成动态sql语句 MyBatis中可以使用OGNL的地方有两处: 动态SQL表达式中 ${param}参数中 上面这两处地方在MyBatis中处理的时候 ...