poj 2774

     求两个串的最长公共子串,后缀数组水题,直接贴代码了。

   

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lrt rt << 1
#define rrt rt << 1|1
#define root 1,n,1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 1e7;
using namespace std;
const double eps = 1e-5;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const double M=1e-8;
const int N=200100;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
int sa[N],rank[N],height[N],len;
int cnt[N],wa[N],wb[N],wv[N];
bool cmp(int *y,int a,int b,int l)
{
    return y[a]==y[b] && y[a+l]==y[b+l];
}
void da(char s[],int n,int m)
{
    int i,j,*x=wa,*y=wb,p;
    MS(cnt,0);
    for (i=0;i<n;i++) cnt[x[i]=s[i]]++;
    for (i=1;i<m;i++) cnt[i]+=cnt[i-1];
    for (i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i;
    for (j=1,p=1;p<n;j<<=1,m=p) {
        for (p=0,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<n;i++) wv[i]=x[y[i]];
        MS(cnt,0);
        for (i=0;i<n;i++) cnt[wv[i]]++;
        for (i=1;i<m;i++) cnt[i]+=cnt[i-1];
        for (i=n-1;i>=0;i--) sa[--cnt[wv[i]]]=y[i];
        swap(x,y);
        for (i=1,p=1,x[sa[0]]=0;i<n;i++) {
            x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
        }
    }
}
void getHeight(char s[],int n)
{
    int i,j,k=0;
    for (i=1;i<=n;rank[sa[i++]]=i) ;
    for (i=0;i<n;height[rank[i++]]=k)
        for (k?k--:0,j=sa[rank[i]-1];s[i+k]==s[j+k];k++) ;
}
void slove(char s[],int n)
{
    int i,j,mx=-INF;
    for (i=1;i<=n;i++) {
        if (height[i]>mx) {
            int a=sa[i-1],b=sa[i];
            if (a>b) swap(a,b);
            if (a<len && b>len) mx=max(mx,height[i]);
        }
    }
    printf("%d\n",mx);
}
int main()
{
    int i,j;
    char s[N];
    while(~scanf("%s",s))
    {
        len=strlen(s);
        strcat(s,"|");
        scanf("%s",s+len+1);
        int n=strlen(s);
        s[n]=0;
        da(s,n+1,128);
        getHeight(s,n);
        slove(s,n);
    }
}


























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值