Substring spoj8222

ppt上的题目,这题主要是练习把每个状态的|right|求出来(还是不理解sam的性质,自己没想到咋求,看了爱神的代码),然后利用每个状态s的|right|更新f(s.len)即f(s.len) = max(|s.right|, f(s.len)),ppt上题解还要用f(i)更新f(i-1),虽然这一步实际上是不必要的,但这样简化了思维的复杂度,题解上用f(i)更新f(i-1)是考虑到如果长度为i的子串一定包含一个长度为i-1的子串,但是根据parent(即自动机里的失配)指针的性质,所有长度为i的子串的|right|必定会更新到一个长度为i-1的子串上的,但可能会出现这种情况即一个状态包含多个长度不同的子串,例如 一个状态表示abb和bb这俩个子串,根绝我们的更新办法,看上去bb的|right|将不会被更新,但注意到abb中还包含ab这个子串,所以只要更新ab的|right|即可,实际上ab也有可能会被忽略掉,但递归的想最终总会找到一个len为2的状态的,所以就不需要再用f(i)更新f(i-1)了。


#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(250010);
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
	{
		NODE *par, *ch[SIGMA_SIZE];
		int val, count;
	};
	NODE *root, *last;
	NODE pool[MAXN << 1];
	int size;
	void init()
	{
		last = root = pool;
		root->par = 0;
		root->val = root->count = 0;
		memset(root->ch, 0, sizeof(root->ch));
		size = 1;
	}
	NODE *newnode(int tv)
	{
		pool[size].val = tv;
		pool[size].count = 0;
		memset(pool[size].ch, 0, sizeof(pool[size].ch));
		return pool+size++;
	}
	void extend(int id)
	{
		NODE *p = last, *np = newnode(last->val+1);
		while(p && p->ch[id] == 0)
			p->ch[id] = np, p = p->par;
		if(p == 0)
			np->par = root;
		else
		{
			NODE *q = p->ch[id];
			if(p->val+1 == q->val)
				np->par = q;
			else
			{
				NODE *nq = newnode(p->val+1);
				memcpy(nq->ch, q->ch, sizeof(nq->ch));
				nq->par = q->par;
				q->par = nq;
				np->par = nq;
				while(p && p->ch[id] == q)
					p->ch[id] = nq, p = p->par;
			}
		}
		last = np;
	}
};

SAM sam;

int cnt[MAXN];
int buc[MAXN << 1];
int table[MAXN];
char str[MAXN];

int main()
{
	while(~scanf("%s", str))
	{
		sam.init();
		int len = strlen(str);
		for(int i = 0; i < len; ++i)
			sam.extend(str[i]-'a');
		memset(cnt, 0, sizeof(cnt[0])*(len+1));
		for(int i = 0; i < sam.size; ++i)
			++cnt[sam.pool[i].val];
		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].val]] = i;
		SAM::NODE *tp = sam.root;
		for(int i = 0; i < len; ++i)
			(tp = tp->ch[str[i]-'a'])->count = 1;
		memset(table, 0, sizeof(table[0])*(len+1));
		for(int i = sam.size; i >= 0; --i)
		{
			tp = sam.pool+buc[i];
			table[tp->val] = max(table[tp->val], tp->count);
			if(tp->par)
				tp->par->count += tp->count;
		}
	/*	for(int i = len-1; i >= 0; --i)
			table[i] = max(table[i], table[i+1]);*/
		for(int i = 1; i <= len; ++i)
			printf("%d\n", table[i]);
	}
	return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值