首页 > 其他分享 >HDU 1403 Longest Common Substring

HDU 1403 Longest Common Substring

时间:2022-11-09 18:33:17浏览次数:47  
标签:HDU size1 int mid Substring maxn 1403 sa rk


Problem Description


Given two strings, you have to tell the length of the Longest Common Substring of them.

For example:
str1 = banana
str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.


 



Input


The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.


 



Output


For each test case, you have to tell the length of the Longest Common Substring of them.


 



Sample Input


banana cianaic


 



Sample Output


3



后缀数组的应用

#include<iostream>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200005;

class suffix
{
private:
char s[maxn];
int r[maxn], w[maxn], ss[maxn], h[maxn];
int sa[maxn], rk[maxn + maxn], size, bit = 256;
int size1;
public:
bool get(){
if (scanf("%s", s + 1) != 1) return false;
size1 = strlen(s + 1);
scanf("%s", s + size1 + 2);
s[++size1] = 1;
size = strlen(s + 1);
return true;
}
void pre()
{
memset(rk, 0, sizeof(rk));
for (int i = 1; i <= bit; i++) w[i] = 0;
for (int i = 1; i <= size; i++) w[(int)s[i]]++;
for (int i = 1; i <= bit; i++) w[i] += w[i - 1];
for (int i = size; i; i--) sa[w[(int)s[i]]--] = i;
for (int i = 1, j = 1; i <= size; i++)
rk[sa[i]] = (s[sa[i]] == s[sa[i + 1]] ? j : j++);
for (int j = 1; j < size; j += j)
{
for (int i = 1; i <= size; i++) w[i] = 0;
for (int i = 1; i <= size; i++) w[rk[i + j]]++;
for (int i = 1; i <= size; i++) w[i] += w[i - 1];
for (int i = size; i; i--) ss[w[rk[i + j]]--] = i;

for (int i = 1; i <= size; i++) w[i] = 0;
for (int i = 1; i <= size; i++) w[rk[ss[i]]]++;
for (int i = 1; i <= size; i++) w[i] += w[i - 1];
for (int i = size; i; i--) sa[w[rk[ss[i]]]--] = ss[i];

for (int i = 1, k = 1; i <= size; i++)
r[sa[i]] = (rk[sa[i]] == rk[sa[i + 1]] && rk[sa[i] + j] == rk[sa[i + 1] + j]) ? k : k++;
for (int i = 1; i <= size; i++) rk[i] = r[i];
}
for (int i = 1, k = 0, j; i <= size; h[rk[i++]] = k)
for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; k++);
}
void work()
{
int mid = (size1 - 1) >> 1;
for (int l = 0, r = size1 - 1,f; l < r;)
{
f = 0;
for (int i = 2, j; i <= size; i = j + 1)
{
int f1 = 0, f2 = 0;
for (j = i; h[j] >= mid; j++)
{
if (sa[j] < size1) f1 = 1; else f2 = 1;
if (sa[j - 1] < size1) f1 = 1; else f2 = 1;
if (f1 && f2) { f = 1; goto end; }
}
}
end:;
if (f) l = mid; else r = mid - 1;
mid = (l + r) >> 1;
if (mid == l) mid = r;
}
printf("%d\n", mid);
}
}f;

int main()
{
while (f.get())
{
f.pre();
f.work();
}
return 0;
}



标签:HDU,size1,int,mid,Substring,maxn,1403,sa,rk
From: https://blog.51cto.com/u_15870896/5837733

相关文章

  • HDU 4658 Integer Partition
    ProblemDescriptionGivenn,k,calculatethenumberofdifferent(unordered)partitionsofnsuchthatnopartisrepeatedkormoretimes.  ......
  • HDU 5638 Toposort
    ProblemDescriptionn verticesand m edges.Youareallowedtodeleteexact k InputT indicatingthenumberoftestcases.Fore......
  • HDU 4651 Partition
    ProblemDescriptionHowmanywayscanthenumbers1to15beaddedtogethertomake15?Thetechnicaltermforwhatyouareaskingisthe"numberofpart......
  • Substring 在BCL和CLR里面搞了啥
    楔子还是做点事情,不要那么散漫。本文以简单的Substring(intstartindex,intLength)函数为例,来递进下它在托管和非托管的一些行为。以下均为个人理解,如有疏漏请指正。......
  • HDU 1028
    如果只有\(1\)数字,多项式为:\(1+x+x^2+x^3+\ldots\)。如果只有\(2\)数字,多项式为:\(1+x^2+x^4+x^6+\ldots\)。……如果只有\(k\)数字,多项式为:\(1+x^k+x^{2k}+x^{3......
  • 学年(2022-2023-1) 学号(20221403)《计算机基础与程序设计》第十周学习总结
    学年(2022-2023-1)学号(20221403)《计算机基础与程序设计》第十周学习总结作业信息这个作业属于哪个课程<班级的链接>(如2022-2023-1-计算机基础与程序设计)这个作业......
  • B - K-th Number HDU - 6231 (二分 尺取)
    WindowsSource2017中国大学生程序设计竞赛-哈尔滨站题意给一个数组,在所有长度大于等于k的区间内,找出第\(k\)大的数,放到另一个数组中,然后在新数组中找到第M大的数。思......
  • HDU-1260 Tickets
    感觉题目还是比较水的,我这个蒟蒻也能写出来hh。思路:f[i]是前i个人(包含第i个)买票需要花费的总时间,第i个人买票所需时间,可以自己单买(f[i-1]+a[i]),也可以和前面那个人拼......
  • HDU 1686Oulipo ———————Hash or KMP
    OulipoOulipoTimeLimit:3000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):22302    AcceptedSubmission(s):86......
  • HDU 2050折线分割平面(递推)
    折线分割平面TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):36479    AcceptedSubmission(s):244......