zoukankan      html  css  js  c++  java
  • HDU 1403-Longest Common Substring (后缀数组)

    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


    题目大意:给两个字符串,求最长的公共子串的长度。
    题目分析:扫一遍height数组即可。

    代码如下:
    # include<iostream>
    # include<cstdio>
    # include<queue>
    # include<cmath>
    # include<string>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    
    const int N=100000;
    
    int SA[N*2+5];
    int cnt[N*2+5];
    int rk[N*2+5];
    int tSA[N*2+5];
    int height[N*2+5];
    int n;
    string str;
    
    bool same(int i,int j,int k)
    {
    	if(tSA[i]!=tSA[j]) return false;
    	if(i+k>=n&&j+k<n) return false;
    	if(i+k<n&&j+k>=n) return false;
    	return tSA[i+k]==tSA[j+k];
    }
    
    void buildSA(string s)
    {
    	int m=27;
    	n=s.size();
    	for(int i=0;i<m;++i) cnt[i]=0;
    	for(int i=0;i<n;++i) ++cnt[rk[i]=s[i]-'a'];
    	for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
    	for(int i=n-1;i>=0;--i) SA[--cnt[rk[i]]]=i;
    	for(int k=1;k<=n;k<<=1){
    		int p=0;
    		for(int i=n-k;i<n;++i) tSA[p++]=i;
    		for(int i=0;i<n;++i) if(SA[i]>=k) tSA[p++]=SA[i]-k;
    		
    		for(int i=0;i<m;++i) cnt[i]=0;
    		for(int i=0;i<n;++i) ++cnt[rk[tSA[i]]];
    		for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
    		for(int i=n-1;i>=0;--i) SA[--cnt[rk[tSA[i]]]]=tSA[i];
    		
    		swap(rk,tSA);
    		p=1;
    		rk[SA[0]]=0;
    		for(int i=1;i<n;++i)
    			rk[SA[i]]=same(SA[i],SA[i-1],k)?p-1:p++;
    		if(p>=n) break;
    		m=p;
    	}
    }
    
    void getHeight()
    {
    	for(int i=0;i<n;++i) rk[SA[i]]=i;
    	int k=0;
    	for(int i=0;i<n;++i){
    		if(rk[i]==0){
    			height[rk[i]]=k=0;
    		}else{
    			if(k) --k;
    			int j=SA[rk[i]-1];
    			while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) ++k;
    			height[rk[i]]=k;
    		}
    	}
    }
    
    string str1,str2;
    
    bool diff(int i,int j,int m)
    {
    	if(SA[i]==m||SA[j]==m) return false;
    	return (SA[i]-m)/abs(SA[i]-m)*(SA[j]-m)/abs(SA[j]-m)<0;
    }
    
    int f(int m)
    {
    	int ans=0;
    	for(int i=1;i<n;++i){
    		if(diff(i,i-1,m)&&height[i]>ans)
    			ans=height[i];
    	}
    	return ans;
    }
    
    int main()
    {
    	while(cin>>str1>>str2)
    	{
    		str=str1+(char)('z'+1)+str2;
    		buildSA(str);
    		getHeight();
    		//cout<<"here is good"<<endl;
    		printf("%d
    ",f(str1.size()));
    	}
    	return 0;
    }
    

      


  • 相关阅读:
    鼠标滑过图片显示放大镜效果
    如何点击iframe跳转以及允许点击全屏展示
    百度echarts饼图百分比的计算规则---最大余额法
    移动端点击出现蓝色背景框&pc端覆盖chrome浏览器input本身的背景颜色
    未解决 --- gorde-map移动端 样式为圆角移动过程中不生效
    Vue -- 数据更新echarts表格不更新问题
    Vue -- table多表头,在表头中添加按钮
    responsive --- a:hover伪类在ios移动端浏览器内无效的解决方法
    编辑器 --- Visual Studio Code mac window 常用快捷键
    mysql 知识整理
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/6049130.html
Copyright © 2011-2022 走看看