2
7
2013
2

后缀自动机……

看一次忘一次,蛋疼死了。

 

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
const int char_size=26;
const int pool_size=100000;
class suffixs_automaton
{
public:
	struct suffix_node{
		bool start;
		int length;
		suffix_node * clean(){
			return this;
		}
		suffix_node*ch[char_size];
		suffix_node*f;
		suffix_node(){
			start=false;
			length=0;
			f=NULL;memset(ch,0,sizeof(ch));
		};
	};
private:
	// 私有变量
	suffix_node suffix_node_pool[pool_size+1];
	int poolsize;
	suffix_node*alloca_node(){ if (poolsize==pool_size) {suffix_node *sn = new suffix_node();assert(sn);}return suffix_node_pool[poolsize++].clean();};
	suffix_node*tail;
	suffix_node*start;
	int length;
public:
	suffixs_automaton(int *c,int len); // [0,len)
	const suffix_node * get_start();
	void add(int c);
private:
	// 内部函数
};
suffixs_automaton::suffixs_automaton(int *c,int len){
	poolsize = 0;
	start = tail = alloca_node();
	start->start=true;length=0;
	for (int i=0;i<len;i++) add(c[i]);
};
const suffixs_automaton::suffix_node * suffixs_automaton::get_start(){
	return start;
};
void suffixs_automaton::add(int c){
	length++;suffix_node*p=tail;suffix_node*np=alloca_node();
	np->length = tail->length+1;
	for (;!!p && !p->ch[c];p=p->f) p->ch[c]=np;
	tail = np;
	if (!p) {np->f=start;return ;}
	if (p->ch[c]->length==p->length+1) np->f=p->ch[c];
	else
	{
		suffix_node*r = alloca_node();suffix_node *q=p->ch[c];
		*r=*q;
		r->length = p->length+1;
		q->f=np->f=r;
		for (;p&&p->ch[c]==q;p=p->f) p->ch[c]=r;
	}
};
int p[]={0,2,0,3,3};
suffixs_automaton SA(p,5);
char c[255];
void dfs(const suffixs_automaton::suffix_node *sn,int l){
	assert(sn);
	if (l!=0) printf("%s\n",c);
	for (int i=0;i<5;i++)
		if (sn->ch[i]){
			c[l]='A'+i;
			dfs(sn->ch[i],l+1);
			c[l]=0;
		}
}
int main(){
	const suffixs_automaton::suffix_node *sn = SA.get_start();
	dfs(sn,0);
	system("pause");
};
Category: OI | Tags: 字符串处理 | Read Count: 1102
civaget 说:
2024年1月11日 04:13

For my part, a person like life-style? Systems work efficiently not waste time, since instance is made up of matter one’s life. how do i make google docs dark mode

civaget 说:
2024年1月17日 07:59

Embrace opga's diverse paths, whether through soothing massages or romantic Kiss Room, and embark on a personal journey of self-discovery.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com