题目链接:hdu_1403_Longest Common Substring

题意:

给你两个字符串,然你找最长的公共子串

题解:

后缀数组的经典应用,要找两个字符串的公共子串,那么就相当于找两个串的后缀的最长公共前缀,我们将两个字符串拼接在一起,中间插一个特殊字符

然后我们考虑height数组,height数组存的是排i和i-1的最长前缀,如果sa[i]和sa[i-1]在特殊字符的两边,那么这个height[i]记录的就是这两个串的最长

子串,然后扫一遍height数组更新一下答案就行了

  1. #include<bits/stdc++.h>
  2. #define F(i,a,b) for(int i=a;i<=b;++i)
  3. using namespace std;
  4.  
  5. namespace suffixarray{
  6. #define FN(n) for(int i=0;i<n;i++)
  7. const int N =2E5+;//字符串长度
  8. int rnk[N],sa[N],height[N],c[N];char s[N];
  9. void getsa(int n,int m,int *x=rnk,int *y=height){
  10. FN(m)c[i]=;FN(n)c[x[i]=s[i]]++;FN(m)c[i+]+=c[i];
  11. for(int i=n-;i>=;i--)sa[--c[x[i]]]=i;
  12. for(int k=,p;p=,k<=n;k=p>=n?N:k<<,m=p){
  13. for(int i=n-k;i<n;i++)y[p++]=i;
  14. FN(n)if(sa[i]>=k)y[p++]=sa[i]-k;
  15. FN(m)c[i]=;FN(n)c[x[y[i]]]++;FN(m)c[i+]+=c[i];
  16. for(int i=n-;i>=;i--)sa[--c[x[y[i]]]]=y[i];
  17. swap(x,y),p=,x[sa[]]=;
  18. for(int i=;i<n;i++)
  19. x[sa[i]]=y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k]?p-:p++;
  20. }
  21. FN(n)rnk[sa[i]]=i;
  22. for(int i=,j,k=;i<n-;height[rnk[i++]]=k)
  23. for(k=k?k-:k,j=sa[rnk[i]-];s[i+k]==s[j+k];k++);
  24. }
  25. }
  26. using namespace suffixarray;
  27. int main(){
  28. while(~scanf("%s",s))
  29. {
  30. int len=strlen(s),sz;
  31. s[len]='$';
  32. scanf("%s",s+len+);
  33. sz=strlen(s),getsa(sz+,);
  34. int ans=;
  35. F(i,,sz)if(ans<height[i]&&(sa[i]-len)*(sa[i-]-len)<)ans=height[i];
  36. printf("%d\n",ans);
  37. }
  38. return ;
  39. }

hdu_1403_Longest Common Substring(后缀数组的应用)的更多相关文章

  1. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

  2. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  3. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  4. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  5. HDU 5769 Substring 后缀数组

    Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...

  6. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  7. 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 ...

  8. POJ3693 Maximum repetition substring 后缀数组

    POJ - 3693 Maximum repetition substring 题意 输入一个串,求重复次数最多的连续重复字串,如果有次数相同的,则输出字典序最小的 Sample input ccab ...

  9. 2016多校联合训练4 F - Substring 后缀数组

    Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...

随机推荐

  1. Voting

    Voting time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  2. 换行符在ajax中返回json,eval时发生的 Unexpected token ILLEGAL

    用户如果输入了换行在数据中记录为‘空格’,但不是真正的空格. 程序前台采用ajax和json返回数据绑定时会 出现 Unexpected token ILLEGAL 例子: 在sql中存储为下图 在“ ...

  3. python+selenium+Eclipse安装

    1.安装python 参考安装python:http://www.cnblogs.com/beyongblue/p/4215740.html 2.安装python管理工具setuptools 3.安装 ...

  4. Application.DoEvents()的作用

    记得第一次使用Application.DoEvents()是为了在加载大量数据时能够有一个数据加载的提示,不至于系统出现假死的现象,当时也没有深入的去研究他的原理是怎样的,结果在很多地方都用上了App ...

  5. css3 div垂直居中

    css3: #dd{ height: 300px; background: #0000cc; display: -webkit-box; display: flex; display: -webkit ...

  6. 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 ...

  7. 一键批量ping任意ip段的存活主机

    =======================by me===================================== @echo offecho.color FC for /f %%i ...

  8. POJ 2234 Matches Game(取火柴博弈1)

    传送门 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  9. P2P直连?经服务器中转?

    当同一个系统的两个客户端A.B相互发送消息给对方时,如果它们之间存在P2P通道,那么消息传送的路径就有两种:直接经P2P通道传送.或者经服务器中转.如下图所示: 通常就一般应用而言,如果P2P通道能够 ...

  10. Python & virtualenv使用说明

    virtualenv是virtual environment的缩写,可以创建独立的Python环境,用起来比较干净:   安装(已安装pip 或者 easy_install): 如果使用pip: pi ...