博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1403 Longest Common Substring
阅读量:5335 次
发布时间:2019-06-15

本文共 2750 字,大约阅读时间需要 9 分钟。

地址:

题目:

Longest Common Substring

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6296    Accepted Submission(s): 2249

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
 

 

Author
Ignatius.L
 

 思路:把两个字符串连接起来,中间用一个没出现过的字符隔开。

  然后二分答案,二分check时对height进行分组,判断height值全大于x的组内 是否同时包含两个字符串的子串

  

1 #include 
2 #include
3 #include
4 #include
5 6 const int N = 200005; 7 int sa[N],s[N],wa[N], wb[N], ws[N], wv[N]; 8 int rank[N], height[N]; 9 10 bool cmp(int r[], int a, int b, int l)11 {12 return r[a] == r[b] && r[a+l] == r[b+l];13 }14 15 void da(int r[], int sa[], int n, int m)16 {17 int i, j, p, *x = wa, *y = wb;18 for (i = 0; i < m; ++i) ws[i] = 0;19 for (i = 0; i < n; ++i) ws[x[i]=r[i]]++;20 for (i = 1; i < m; ++i) ws[i] += ws[i-1];21 for (i = n-1; i >= 0; --i) sa[--ws[x[i]]] = i;22 for (j = 1, p = 1; p < n; j *= 2, m = p)23 {24 for (p = 0, i = n - j; i < n; ++i) y[p++] = i;25 for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;26 for (i = 0; i < n; ++i) wv[i] = x[y[i]];27 for (i = 0; i < m; ++i) ws[i] = 0;28 for (i = 0; i < n; ++i) ws[wv[i]]++;29 for (i = 1; i < m; ++i) ws[i] += ws[i-1];30 for (i = n-1; i >= 0; --i) sa[--ws[wv[i]]] = y[i];31 for (std::swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)32 x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;33 }34 }35 36 void calheight(int r[], int sa[], int n)37 {38 int i, j, k = 0;39 for (i = 1; i <= n; ++i) rank[sa[i]] = i;40 for (i = 0; i < n; height[rank[i++]] = k)41 for (k?k--:0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++);42 }43 bool check(int la,int lb,int lc,int x)44 {45 int m1=0,m2=0;46 if(sa[1]
la)m2=1;48 for(int i=2;i<=lc;i++)49 {50 if(height[i]
la)m2=1;58 }59 return m1&&m2;60 }61 char ss[N];62 int main()63 {64 while(scanf("%s",ss)==1)65 {66 int la=strlen(ss),lb,n=0;67 for(int i=0;i
>1;81 if(check(la,lb,n,mid))82 ans=mid,l=mid+1;83 else84 r=mid-1;85 }86 printf("%d\n",ans);87 }88 return 0;89 }

 

转载于:https://www.cnblogs.com/weeping/p/6648830.html

你可能感兴趣的文章
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
协议和代理
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法(转)...
查看>>
[转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
查看>>
Spring mvc初学
查看>>
python标准库学习7
查看>>
有意思的代码片段
查看>>
C8051开发环境
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
255. Verify Preorder Sequence in Binary Search Tree
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
[fowarding]Ubuntu jsp平台使用JDBC来连接MySQL数据库
查看>>
函数式编程
查看>>
JavaScript中的BOM和DOM
查看>>
bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草
查看>>
[转]AngularJS:何时应该使用Directive、Controller、Service?
查看>>