本文主要是介绍HDU - 1403 Longest Common Substring,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.题面
http://acm.hdu.edu.cn/showproblem.php?pid=1403
2.题意
求两个字符串的最长公共子串
3.思路
先将两个串用特殊字符隔开后连接
用后缀数组,考察两个连续的Suffix(sa[i]),如果来自两个不同的串就将用height[i+1]更新答案
4.代码
/*****************************************************************> File Name: Cpp_Acm.cpp> Author: Uncle_Sugar> Mail: uncle_sugar@qq.com> Created Time: 2016年07月29日 星期五 20时13分08秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;template<class T>void PrintArray(T* first,T* last,char delim=' '){ for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}const int debug = 1;
const int size = 10 + 200000 ;
const int INF = INT_MAX>>1;
typedef long long ll;const int MAXN = 2*100000+10;int t1[MAXN],t2[MAXN],c[MAXN];bool cmp(int *r,int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];
}void da(char str[],int sa[],int rrank[],int height[],int n,int m){n++;int i, j, p, *x = t1, *y = t2;for (i = 0;i < m;i++) c[i] = 0;for (i = 0;i < n;i++) c[x[i] = str[i]]++;for (i = 1;i < m;i++) c[i] += c[i-1];for (i = n-1;i >= 0;i--) sa[--c[x[i]]] = i;for (j = 1;j <= n; j<<= 1){p = 0;for (i = n-j; i < n; i++) y[p++] = i;for (i = 0; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j; for (i = 0; i < m; i++) c[i] = 0;for (i = 0; i < n; i++) c[x[y[i]]]++;for (i = 1; i < m; i++) c[i] += c[i-1];for (i = n-1;i >= 0; i--) sa[--c[x[y[i]]]] = y[i];swap(x,y);p = 1; x[sa[0]] = 0;for (i = 1; i < n; i++){x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;}if ( p>=n ) break;m = p;}int k = 0;n--;for (i = 0; i<= n; i++) rrank[sa[i]] = i;for (i = 0; i< n; i++){if (k) k--;j = sa[rrank[i]-1];while (str[i+k] == str[j+k]){k++;}height[rrank[i]] = k;}
}int rrank[MAXN],height[MAXN];
int sa[MAXN];char str[size];int main()
{std::ios::sync_with_stdio(false);cin.tie(0);int i,j;while (cin >> str){int len1 = strlen(str);str[len1] = 1;cin >> (str+len1+1);int len = len1;while (str[len]!='\0')len++;//# cout << "len = " << len << endl;//# cout << "len1= " << len1 << endl;//# for (i=0;i<len;i++){//# cout << char(str[i]);//# }//# cout << endl;//# void da(int str[],int sa[],int rrank[],int height[],int n,int m){da(str,sa,rrank,height,len,128);//# for (i=1;i<=len;i++){//# cout << "*" << i << "* ";//# for (j=sa[i];j<len;j++){//# cout << char(str[j]);//# }//# cout << endl;//# }//# for (i=1;i<=len;i++) cout << i << '\t';//# cout << endl;//# PrintArray(height+1,height+len+1,'\t');//# PrintArray(sa+1,sa+len+1,'\t');int ans = 0;//# cout << "height[6] " << height[6] << endl; for (i=1;i<=len;i++){//# cout << "i = " << i << endl; //# cout << "sa i i+1 " << sa[i] << " " << sa[i+1] << endl;if ((sa[i]>=len1)!=(sa[i+1]>=len1)){//# cout << "i = " << i << " " << sa[i+1] << " " << height[sa[i+1]] << endl;ans = max(ans,height[i+1]);}}cout << ans << endl;}return 0;
}
这篇关于HDU - 1403 Longest Common Substring的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!