hdu 1403 Longest Common Substring (后缀数组)
来源:互联网 发布:java软件开发年终总结 编辑:程序博客网 时间:2024/06/07 05:34
Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5776 Accepted Submission(s): 2052
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.
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.
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
bananacianaic
Sample Output
3
题目的意思是求两个字符串的最大匹配长度
思路 将两个串A .B用一个绝对小的字符连接成一个串C
求串C的后缀数组 在在这个里面找出两个分别代表A B 串的公共前缀的最长的那个
(数据蛮水 这样居然过了)
#include<iostream>#include<algorithm>#include<cstdlib>#include<cctype>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<set>#include<map>#include<queue>#include<cmath>#include<bitset>#define pi acos(-1.0)#define inf 1<<29#define INF 0x3fffffff#define zero 1e-8const int li[]={-1,0,1,0};const int lj[]={0,-1,0,1};using std::cout;using std::endl;const int N=200000+107;char str[2*N];int len1,len2,len;int rank[2 * N], SA[2 * N];int tem[2 * N];int ccnt[N];int height[N];inline bool cmp(int i,int cnt){ return rank[SA[i]+cnt]==rank[SA[i-1]+cnt]&&rank[SA[i]]==rank[SA[i-1]];}inline bool judge(int i,int j){ return (i<len1&&j>len1)||(i>len1&&j<len1);}void show(){ str[len]='\0'; for (int i=0 ;i<=len;++i) std::cout<<i<<" SA: "<<SA[i]<<" "<<str+SA[i]<<std::endl;//<<" rank: "<<rank[i]<<std::endl;}void da(int MaxDataOfBegin){ memset(tem,0,sizeof(tem)); for (int i = 0; i < len; ++i){ tem[str[i]] = true; } for (int i = 1; i < MaxDataOfBegin+1; ++i){ tem[i] += tem[i - 1]; } for (int i = 0; i < len; ++i) rank[i] = tem[str[i]]; for (int i = len; i < 2 * len; ++i) rank[i] = 0; for (int lon = 2, cnt = 1; ; lon <<= 1, cnt<<=1) { for (int i=0;i<=len;ccnt[i++]=0); for (int i = cnt; i < len + cnt; ++i) ccnt[rank[i]]++; for (int i = 1; i <= len; ++i){ ccnt[i] += ccnt[i - 1]; } for (int i = len + cnt-1; i >= cnt; --i) { tem[ccnt[rank[i]]-1] = i - cnt; ccnt[rank[i]]--; } for (int i=0;i<=len;ccnt[i++]=0); for (int i = 0; i < len; ++i) ccnt[rank[tem[i]]]++; for (int i = 1; i <= len; ++i) ccnt[i] += ccnt[i - 1]; for (int i = len-1; i >=0; --i) { SA[ccnt[rank[tem[i]]]-1] = tem[i]; ccnt[rank[tem[i]]]--; } int rk=1; tem[SA[0]] = rk; for (int i=1;i<len;++i){ if (!cmp(i,cnt)) rk++; tem[SA[i]]=rk; } for (int i=0;i<len;++i) rank[i]=tem[i]; if (lon>=len) break; }}int calheight(){ for (int i=0;i<=len;height[i++]=0); for (int i=2;i<len;++i){ if (judge(SA[i-1],SA[i])){ int lon=0; for (;(SA[i]+lon<len1||SA[i-1]+lon<len1) &&str[SA[i]+lon]==str[SA[i-1]+lon];lon++); height[i]=height[i]>lon?height[i]:lon; height[i-1]=lon; } } int maxx=0; for(int i=1;i<len;++i) maxx=maxx>height[i]?maxx:height[i]; return maxx;}int main(){ while(~scanf("%s",str)){ len1=strlen(str); str[len1]='Q'; scanf("%s",str+len1+1); len=strlen(str); da('z'); printf("%d\n",calheight()); } return 0;}
0 0
- HDU 1403 Longest Common Substring 【 后缀数组】
- hdu 1403 Longest Common Substring - 后缀数组
- hdu 1403 Longest Common Substring (后缀数组)
- hdu 1403 Longest Common Substring (后缀数组模板题)
- hdu 1403 Longest Common Substring (后缀数组模板)
- hdu 1403 Longest Common Substring 后缀数组da算法
- HDU 1403 Longest Common Substring(后缀数组入门)
- 【后缀数组】 HDOJ 1403 Longest Common Substring
- poj2774 Long Long Message && hdu 1403 Longest Common Substring 最长公共字串【后缀数组(倍增)】
- HDU 1403 Longest Common Substring(后缀数组 最长公共子串)
- POJ 2774 Long Long Message+Hdu 1403 Longest Common Substring (后缀数组 最长公共子串)
- poj - 2774 - Long Long Message / hdu - 1403 - Longest Common Substring(后缀数组)
- hdu 1403 Longest Common Substring 后缀数组求最长公共子串
- hdu 1403 Longest Common Substring 后缀数组求最长公共子串
- HDOJ 1403:Longest Common Substring 后缀数组的用处
- HDU1403 Longest Common Substring 后缀数组
- 后缀数组 spoj LCS - Longest Common Substring
- HDU 1403 Longest Common Substring
- TCP和UDp
- 【 逆元 】
- flume+kafka+storm+mysql架构设计
- Oracle启动和关闭命令
- 切片
- hdu 1403 Longest Common Substring (后缀数组)
- PhpExcel中文帮助手册|PhpExcel使用方法
- 编译提示没有生成object文件
- sts创建maven项目+创建经典source结构目录
- 把自己当做用户,不是让你代表用户
- 新版flume+kafka+storm安装部署
- Oracle用户和权限
- 学密码学一定得学程序
- HttpConnention