SPOJ Longest Common Substring II 后缀自动机(打印)

给无数个串,问你他们合起来的公共子串最长是多少。


比人说WA10就不理解后缀自动机,我觉得我不理解后缀自动机居然AC了……



1、用第一个串做出后缀自动机

2、之后每一个串,都可以在自动机上跑。

3、一个串,在自动机上跑,可以知道  "走到这个状态,走了几步。同时!!这个步数要取max!" 一个状态如果能到达,那么这个状态的父状态一定可达(并且一定存在步数为父状态的len的方案)。


4、所以我们跑完自动机后,利用反拓扑序DP的方法,可以得到所有前序状态的步数(就等于len值)。 


5、我们就知道,一个串在自动机上到所有状态的最大步数。 把N个串都跑一次,取min,输出即可。

(比赛要打印这份代码带着!)


#include 
#include 
#include 
#include 
using namespace std;


const int CHAR = 26;
const int MAXN = 250000 * 2  + 100;
char str[MAXN];

bool duanyan(bool flag)
{
	if (!flag)
	{
		throw 0;
	}
}

struct SAM_Node
{
	SAM_Node *fa, *next[CHAR];
	int len;
	int id, pos;
	int dp, ans;
	SAM_Node(){}
	SAM_Node(int _len)
	{
		fa = 0;
		len = _len;
		dp=0;
		ans = 1000000000;
		memset(next, 0, sizeof(next));
	}
};

SAM_Node SAM_node[MAXN *2], *SAM_root, *SAM_last;
int SAM_size;

SAM_Node * newSAM_Node(int len)
{
	SAM_node[SAM_size] = SAM_Node(len);
	SAM_node[SAM_size].id = SAM_size; 
	SAM_node[SAM_size].dp = 0; 
	return &SAM_node[SAM_size++];
}

SAM_Node *newSAM_Node(SAM_Node *p)
{
	SAM_node[SAM_size] = *p;
	SAM_node[SAM_size].id = SAM_size;
	SAM_node[SAM_size].dp = 0; 
	return &SAM_node[SAM_size++];
}

void SAM_init()
{
	SAM_size = 0;
	SAM_root = SAM_last = newSAM_Node(0);
	SAM_node[0].pos = 0;
}

void SAM_add(int x, int len)
{
	SAM_Node *p = SAM_last, *np = newSAM_Node(p -> len+1);
	np -> pos = len;
	SAM_last = np;
	for (;p && !p -> next[x]; p = p -> fa)
		p -> next[x] = np;
	if (!p)
	{
		np -> fa = SAM_root;
		return;
	}
	SAM_Node *q = p -> next[x];
	if (q -> len == p-> len+1)
	{
		np -> fa = q;
		return;
	}
	SAM_Node *nq = newSAM_Node(q);
	nq -> len = p -> len + 1;
	q -> fa = nq;
	np -> fa = nq;
	for (;p && p-> next[x] == q; p = p -> fa)
		p -> next[x] = nq;
}

int topcnt[MAXN];
SAM_Node *topsam[MAXN*2];

void SAM_build(char *s)
{
	SAM_init();
	int len = strlen(s);
	for (int i = 0 ;i < len; ++ i)
		SAM_add(s[i] - 'a' , i + 1);
}

int g[MAXN], f[MAXN];

void SAM_search()
{
	SAM_Node *now = SAM_root;
	int len = strlen(str);
	int ans = 0;
	for (int i = 0; i < SAM_size; ++ i)
	{
		SAM_node[i].dp = 0;
	}

	for (int i = 0; i < len; ++ i)
	{
		int t = str[i] - 'a';
		if (now -> next[t])
		{
			now =now -> next[t];
			++ans;
		}
		else
		{
			while (now != SAM_root && !now -> next[t])	now = now -> fa;
			if (now -> next[t])
			{
				ans = now -> len + 1;
				now = now -> next[t];
			}
			else
			{
				now = SAM_root;
				ans = 0;
			}

		}
		now -> dp = max(ans, now->dp);
	}

	for (int i = SAM_size - 1; i > 0; -- i)
	{
		SAM_Node *now = topsam[i];
		if (now -> dp)
		now -> fa -> dp = max(now -> fa -> dp, now -> fa -> len);
	}

	for (int i = 0; i < SAM_size; ++ i)
	{
		SAM_node[i].ans = min(SAM_node[i].ans, SAM_node[i].dp);
	}



}

void pg()
{

	for (int i = 0; i < SAM_size; ++ i)
	{
		cout <<"@"<< SAM_node[i].id<< endl;
		for (int j = 0; j < 26; ++ j)
		{
			if (SAM_node[i].next[j])
			{
				cout << SAM_node[i].next[j] -> id << endl;
			}
		}
		if (i)
		cout << "fa: " << SAM_node[i].fa -> id << endl;
		cout<<"===="<


你可能感兴趣的:(SPOJ Longest Common Substring II 后缀自动机(打印))