后缀数组模板(求最长公共子串,hdu1403)

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 200010;

int r[N], tx[N], ty[N], rs[N], ranks[N], sa[N], height[N], rmq[N][20]; //rs基数排序
char s[N];

bool cmp(int *r, int a, int b, int len)
{
	return (r[a] == r[b]) && (r[a + len] == r[b + len]);
}

void suffix(int n, int m) //n为长度,最大值小于m
{
	int i, j, p, *x = tx, *y = ty, *t;
	for(i = 0; i < m; ++i)
		rs[i] = 0;
	for(i = 0; i < n; ++i) {
		x[i] = r[i];
		++rs[x[i]];
	}
	for(i = 1; i < m; ++i)
		rs[i] += rs[i - 1];
	for(i = n - 1; i >= 0; --i)
		sa[--rs[x[i]]] = i;
	for(j = 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 < m; ++i)
			rs[i] = 0;
		for(i = 0; i < n; ++i)
			++rs[x[y[i]]];
		for(i = 1; i < m; ++i)
			rs[i] += rs[i - 1];
		for(i = n - 1; i >= 0; --i)
			sa[--rs[x[y[i]]]] = y[i];
		t = x, x = y, y = t;
		for(i = 1, p = 1, x[sa[0]] = 0; i < n; ++i) {
			if(cmp(y, sa[i - 1], sa[i], j))
				x[sa[i]] = p - 1;
			else
				x[sa[i]] = p++;
		}
	}
	/**for(i = 0; i < n; ++i)
	printf("%s\n", s + sa[i]);*/
}

void calheight(int n)
{
	int i, j, k = 0;
	for(i = 1; i <= n; ++i)
		ranks[sa[i]] = i;
	for(i = 0; i < n; ++i) {
		if(k)
			--k;
		j = sa[ranks[i] - 1];
		while(r[i + k] == r[j + k])
			++k;
		height[ranks[i]] = k;
	}
}

void initrmq(int n)
{
	int i, k;
	for(i = 2; i <= n; ++i)
		rmq[i][0] = height[i];
	for(k = 1; (1 << k) <= n; ++k) {
		for(i = 2; i + (1 << k) - 1 <= n; ++i) {
			rmq[i][k] = min(rmq[i][k - 1],
				rmq[i + (1 << (k - 1))][k - 1]);
		}
	}
}
int Log[N];
void initlog()
{
Log[0] = -1;  
    for(int i=1;i<N;i++){  
        Log[i]=(i&(i-1))?Log[i-1]:Log[i-1] + 1 ;}
}

int lcp(int a, int b)
{
	a = ranks[a], b = ranks[b];
	if(a > b)
		swap(a, b);
	++a;
	int k = (int) Log[b - a + 1] / Log[2];
	return min(rmq[a][k], rmq[b - (1 << k) + 1][k]);
}
//求两个串的最长公共子串

int main()
{
	while( scanf("%s", s)!=EOF)
	{
		int i, l1, n, ans;
		l1 = strlen(s);
		s[l1] = 1;
		scanf("%s", s + l1 + 1);
		n = strlen(s);
		for(i = 0; i < n; ++i)
			r[i] = s[i];
		r[n] = 0;//便于比较
		suffix(n + 1, 128);
		calheight(n);
		ans = 0;
		for(i = 2; i <= n; ++i) {
			if((height[i] > ans) &&
				((sa[i - 1] < l1 && sa[i] > l1) ||
				(sa[i - 1] > l1 && sa[i] < l1)))
				ans = height[i];
		}
		printf("%d\n", ans);
	}
	system("pause");
	return 0;
}

新版
<pre name="code" class="cpp">#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 200010;
char s[N];
int r[N], tx[N], ty[N], rs[N], ranks[N], sa[N], height[N], rmq[N][20]; //rs基数排序

bool cmp(int *r, int a, int b, int len)
{
	return (r[a] == r[b]) && (r[a + len] == r[b + len]);
}
void suffix(int n, int m) //n为长度,最大值小于m
{
	int i, j, p, *x = tx, *y = ty, *t;
	for(i = 0; i < m; ++i) rs[i] = 0;
	for(i = 0; i < n; ++i) { x[i] = r[i]; ++rs[x[i]];}
	for(i = 1; i < m; ++i) rs[i] += rs[i - 1];
	for(i = n - 1; i >= 0; --i) sa[--rs[x[i]]] = i;
	for(j = 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 < m; ++i) rs[i] = 0;
		for(i = 0; i < n; ++i) ++rs[x[y[i]]];
		for(i = 1; i < m; ++i) rs[i] += rs[i - 1];
		for(i = n - 1; i >= 0; --i) sa[--rs[x[y[i]]]] = y[i];
		t = x, x = y, y = t;
		for(i = 1, p = 1, x[sa[0]] = 0; i < n; ++i) {
			if(cmp(y, sa[i - 1], sa[i], j)) x[sa[i]] = p - 1;
			else x[sa[i]] = p++;
		}
	}
}

void calheight(int n)
{
	int i, j, k = 0;
	for(i = 1; i <= n; ++i)
		ranks[sa[i]] = i;
	for(i = 0; i < n; ++i) {
		if(k)
			--k;
		j = sa[ranks[i] - 1];
		while(r[i + k] == r[j + k])
			++k;
		height[ranks[i]] = k;
	}
}

//rmq 求 lcp
void initrmq(int n)
{
	int i, k;
	for(i = 2; i <= n; ++i)
		rmq[i][0] = height[i];
	for(k = 1; (1 << k) <= n; ++k) {
		for(i = 2; i + (1 << k) - 1 <= n; ++i) {
			rmq[i][k] = min(rmq[i][k - 1],
				rmq[i + (1 << (k - 1))][k - 1]);
		}
	}
}

int Log[N];
void initlog()
{
	Log[0] = -1;  
	for(int i=1;i<N;i++)  
		Log[i]=(i&(i-1))?Log[i-1]:Log[i-1] + 1;
}

int lcp(int a, int b ,int n)//求a,b的后缀的公用前缀长度,从0计
{
	if(a==b) return n-a;
	a = ranks[a], b = ranks[b];
	if(a > b)
		swap(a, b);
	++a;
	int k = (int) Log[b - a + 1] / Log[2];
	return min(rmq[a][k], rmq[b - (1 << k) + 1][k]);
}

求第k小串(忽略重复)

__int64 effectNum[N],sumEffectNum[N],allNum;//分别表示sa中的i后边有效串数,前i总有效串数;allNum为总共不同串的个数
int aimSl,aimSr,aimSlength;//目标串左右端点以及长度
void stStringInit(int n)
{
	sumEffectNum[0]=0;
	for(int i=1;i<=n;i++)
	{
		effectNum[i]=n-sa[i]-height[i];	
		sumEffectNum[i]=sumEffectNum[i-1]+effectNum[i];
	}
	allNum=sumEffectNum[n];
}

int aimSbs(int left,int right,__int64 v)
{
	int mid;
	while(left<right)
	{
		mid=(left+right)>>1;
		if(sumEffectNum[mid]<v) left=mid+1;//if(不符合条件)
		else right=mid;
	}
	return left;
}

void getAimString(__int64 aimst,int n)
{
	int temp;
	temp=aimSbs(1,n,aimst);
	aimSl=sa[temp];
	aimSr=aimSl+height[temp]+aimst-sumEffectNum[temp-1]-1;
	aimSlength=aimSr-aimSl+1;
}

//

int main()
{
	//initlog();
	while( scanf("%s", s)!=EOF)
	{
		int i,j,n;
		n = strlen(s);
		for(i = 0; i < n; ++i)
			r[i] = s[i];
		r[n] = 0;//便于比较
		suffix(n + 1, 128);
		calheight(n);
		//initrmq(n);
	}
	return 0;
}

/*
simple:
	ababcaaabc
	n=10

st  string    ranks   /  sa   saString    height
0  ababcaaabc   3     /  10   0            0
1  babcaaabc    6     /  5    aaabc        0
2  abcaaabc     5     /  6    aabc         2
3  bcaaabc      8     /  0    ababcaaabc   1
4  caaabc       10    /  7    abc          2
5  aaabc        1     /  2    abcaaabc     3
6  aabc         2     /  1    babcaaabc    0
7  abc          4     /  8    bc           1
8  bc           7     /  3    bcaaabc      2
9  c            9     /  9    c            0
10 0            0     /  4    caaabc       1
*/


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值