LCS2 spoj1812

这题还是要理解parent指针转移后的状态,每个该状态表示的子串都是合法的。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;

const int MAXN(100010);
const int SIGMA_SIZE(26);
const int MAXM(110);
const int MAXE(300010);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const int MOD(2520);
const ULL BASE(31);
const ULL LIM(1000000000000000ull);

struct SAM
{
	struct NODE
	{
		int len, ans1, ans2; //ans1表示所有之前的串在该状态表示的子串中共有的长度最大的,ans2表示当前串与第一个串在该状态表示的子串中共有的长度最大的
		NODE *f, *ch[SIGMA_SIZE];
	};
	NODE pool[MAXN << 1];
	NODE *root, *last;
	int size;
	void init()
	{
		last = root = pool;
		root->len = 0;
		root->ans1 = 0;
		root->ans2 = 0;
		root->f = 0;
		memset(root->ch, 0, sizeof(root->ch));
		size = 1;
	}
	NODE *newnode(int tl)
	{
		pool[size].len = tl;
		pool[size].ans1 = tl;
		pool[size].ans2 = 0;
		memset(pool[size].ch, 0, sizeof(pool[size].ch));
		return pool+size++;
	}
	void extend(int id)
	{
		NODE *p = last, *np = newnode(last->len+1);
		last = np; //别忘了
		while(p && p->ch[id] == 0)
			p->ch[id] = np, p = p->f;
		if(p == 0)
			np->f = root;
		else
		{
			NODE *q = p->ch[id];
			if(p->len+1 == q->len)
				np->f = q;
			else
			{
				NODE *nq = newnode(p->len+1);
				memcpy(nq->ch, q->ch, sizeof(nq->ch));
				nq->f = q->f;
				q->f = np->f = nq;
				while(p && p->ch[id] == q)
					p->ch[id] = nq, p = p->f;
			}
		}
	}
};

SAM sam;
char str[MAXN];
int cnt[MAXN], buc[MAXN << 1];

int main()
{
	sam.init();
	scanf("%s", str);
	for(char *tp = str; *tp; ++tp)
		sam.extend(*tp-'a');
	int len = strlen(str);
	for(int i = 0; i < sam.size; ++i) ++cnt[sam.pool[i].len];
	for(int i = 1; i <= len; ++i) cnt[i] += cnt[i-1];
	for(int i = 0; i < sam.size; ++i) buc[--cnt[sam.pool[i].len]] = i;
	while(~scanf("%s", str))
	{
		int l = 0;
		SAM::NODE *p = sam.root;
		for(char *tp = str; *tp; ++tp)
		{
			int id = *tp-'a';
			if(p->ch[id])
			{
				++l;
				p = p->ch[id];
			}
			else
			{
				while(p && p->ch[id] == 0)
					p = p->f;
				if(p)
				{
					l = p->len+1;
					p = p->ch[id];
				}
				else
				{
					l = 0;
					p = sam.root;
				}
			}
			p->ans2 = max(p->ans2, l);
		}
		for(int i = sam.size-1; i >= 0; --i)
		{
			p = sam.pool+buc[i];
			p->ans1 = min(p->ans1, p->ans2);
			if(p->f && p->ans2)
				p->f->ans2 = p->f->len;  //利用失配指针转移,一定可以取到max
			p->ans2 = 0;
		}
	}
	int ans = 0;
	for(int i = 0; i < sam.size; ++i)
		ans = max(ans, sam.pool[i].ans1);
	printf("%d\n", ans);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值