POJ 2774 Long Long Message

Description

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. 

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 

1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. 

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. 

Background: 
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. 

Why ask you to write a program? There are four resions: 
1. The little cat is so busy these days with physics lessons; 
2. The little cat wants to keep what he said to his mother seceret; 
3. POJ is such a great Online Judge; 
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output

27



后缀数组求两个串的最长公共子串,二分长度+验证

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int read()
{
	char ch = getchar();
	while (ch<'0' || ch>'9') ch = getchar();
	int x = ch - '0';
	while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
	return x;
}
int T = 0;

struct Sa
{
	char s[N];
	int rk[2][N], sa[N], h[N], w[N], now, n;
	int rmq[N][20], lg[N], bel[N];

	bool GetS()
	{
		if (scanf("%s", s + 1) == -1) return false;
		for (n = 1; s[n]; n++) bel[n] = 0;
		bel[n++] = 2;
		scanf("%s", s + n);
		while (s[n]) bel[n++] = 1;
		--n;
		return true;
	}

	void getsa(int z, int &m)
	{
		int x = now, y = now ^= 1;
		rep(i, 1, z) rk[y][i] = n - i + 1;
		for (int i = 1, j = z; i <= n; i++)
			if (sa[i] > z) rk[y][++j] = sa[i] - z;

		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[rk[x][rk[y][i]]]++;
		rep(i, 1, m) w[i] += w[i - 1];
		per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
		for (int i = m = 1; i <= n; i++)
		{
			int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
			rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
		}
	}

	void getsa(int m)
	{
		//n = strlen(s + 1);
		rk[1][0] = now = sa[0] = s[0] = 0;
		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[s[i]]++;
		rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];
		rep(i, 1, m) w[i] += w[i - 1];
		rep(i, 1, n) rk[0][i] = rk[1][s[i]];
		rep(i, 1, n) sa[w[s[i]]--] = i;

		rk[1][n + 1] = rk[0][n + 1] = 0;	//多组的时候容易出bug
		for (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y);
		for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j)
		{
			if (rk[now][i] == 1) continue;
			int k = n - max(sa[rk[now][i] - 1], i);
			while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
		}
	}

	void getrmq()
	{
		h[n + 1] = h[1] = lg[1] = 0;
		rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
		for (int i = 1; (1 << i) <= n; i++)
		{
			rep(j, 2, n)
			{
				if (j + (1 << i) > n + 1) break;
				rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
			}
		}
	}

	int lcp(int x, int y)
	{
		int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
		return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
	}

	bool check(int x)
	{
		int f[3] = { 0,0,0 };
		rep(i, 2, n)
		{
			if (h[i] >= x) f[bel[sa[i]]] = f[bel[sa[i - 1]]] = 1;
			else
			{
				if (f[0] && f[1]) return true;
				f[0] = f[1] = 0;
			}
		}
		return f[0] && f[1];
	}

	void work()
	{
		getsa(300);
		int l = 1, r = n;
		while (l <= r)
		{
			if (check(l + r >> 1)) l = (l + r >> 1) + 1;
			else r = (l + r >> 1) - 1;
		}
		printf("%d\n", r);
	}
}sa;

int main()
{
	while (sa.GetS()) sa.work();
	return 0;
}

二分+双重hash判重
#include<map>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+10;
const int mod=1e9+7;
const int Mod=1e5+3;
char a[N],b[N];
long long f[N],g[N],c[N],d[N];
long long ff[N],gg[N],cc[N],dd[N];

long long n,m,w[N];
int ft[N],nt[N],u[N],sz;

long long inv(long long x)
{
    return x==1?x:1LL*inv(mod%x)*(mod-mod/x)%mod;
}

long long Inv(long long x)
{
    return x==1?x:1LL*Inv(Mod%x)*(Mod-Mod/x)%Mod;
}

bool check(int x)
{
    sz=0;
    for (int i=0;i<Mod;i++) ft[i]=-1;
    for (int i=1;i<=n-x+1;i++)
    {
        long long y = 1LL*(f[i+x-1]-f[i-1]+mod)*c[i-1]%mod;
        long long yy = 1LL*(ff[i+x-1]-ff[i-1]+Mod)*cc[i-1]%Mod;
        u[sz]=y; nt[sz]=ft[yy]; ft[yy]=sz++;
    }
    for (int i=1;i<=m-x+1;i++)
    {
        long long y = 1LL*(g[i+x-1]-g[i-1]+mod)*c[i-1]%mod;
        long long yy = 1LL*(gg[i+x-1]-gg[i-1]+Mod)*cc[i-1]%Mod;
        for (int j=ft[yy];j!=-1;j=nt[j])
        {
            if (u[j]==y) return true;
        }
    }
    return false;
}

int main()
{
    c[0]=1; c[1]=inv(131);
    for (int i=2;i<N;i++) c[i]=1LL*c[i-1]*c[1]%mod;
    d[0]=1;
    for (int i=1;i<N;i++) d[i]=131LL*d[i-1]%mod;

    cc[0]=1; cc[1]=Inv(131);
    for (int i=2;i<N;i++) cc[i]=1LL*cc[i-1]*cc[1]%Mod;
    dd[0]=1;
    for (int i=1;i<N;i++) dd[i]=131LL*dd[i-1]%Mod;

    while (~scanf("%s%s",a+1,b+1))
    {
        n=strlen(a+1); m=strlen(b+1);
        f[0]=g[0]=0;
        ff[0]=gg[0]=0;
        for (int i=1;i<=n;i++)
        {
            f[i]=(f[i-1]+1LL*a[i]*d[i-1])%mod;
            ff[i]=(ff[i-1]+1LL*a[i]*dd[i-1])%Mod;
        }
        for (int i=1;i<=m;i++)
        {
            g[i]=(g[i-1]+1LL*b[i]*d[i-1])%mod;
            gg[i]=(gg[i-1]+1LL*b[i]*dd[i-1])%Mod;
        }
        int l=0,r=min(n,m);
        while (l<=r)
        {
            int mid=l+r>>1;
            if (check(mid)) l=mid+1; else r=mid-1;
        }
        printf("%d\n",r);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值