Common Substring poj 3415 (SAM 해법)

이 문 제 는 SA 로 하 는 것 이 매우 슬 프 고 SAM 으로 하 는 것 이 SA 로 하 는 것 보다 간단 하 다. 이런 일련의 계수 류 의 문 제 는 비교적 번 거 로 운 문제 에 속 하기 때문에 기록 해 야 할 정보 가 비교적 많다. 먼저 모든 상태의 min 과 | right | 를 미리 처리 하고 네 개의 추가 정 보 를 기록 해 야 한다. 코드 설명 을 참조 하 자.
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include    
#include 
#include   
#include  
#include 
#include     
#include 
#include    
#include 

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::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair PAIR;
typedef multimap MMAP;
typedef LL TY;

const int MAXN(100010);
const int MAXM(50010);
const int MAXE(100010);
const int MAXK(6);
const int HSIZE(131313);
const int SIGMA_SIZE(52);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);

template void checkmax(T &a, T b){if(b > a) a = b;}
template void checkmin(T &a, T b){if(b < a) a = b;}
template T ABS(const T &a){return a < 0? -a: a;}

char str[MAXN];
int K;
int cnt[MAXN];
int buc[MAXN << 1];
int hash[128];

struct SAM
{
	struct NODE
	{
		int len, mi, right;
		LL cnt;
		NODE *f, *ch[SIGMA_SIZE];
	};
	NODE *root, *last;
	NODE pool[MAXN << 1];
	int size;
	void init()
	{
		root = last = pool;
		root->f = 0;
		root->len = 0;
		root->cnt = 0;
		root->len = root->mi = root->right = 0;
		memset(root->ch, 0, sizeof(root->ch));
		size = 1;
	}
	
	NODE *newnode(int tl)
	{
		pool[size].len = tl;
		pool[size].cnt = 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);
		np->right = 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->right = 0;
				nq->f = q->f;
				q->f = np->f = nq;
				while(p && p->ch[id] == q)
					p->ch[id] = nq, p = p->f;
			}
		}
	}
	void tupo()
	{
		int tl = last->len;
		memset(cnt, 0, sizeof(cnt[0])*(tl+1));
		for(int i = 0; i < size; ++i) ++cnt[pool[i].len];
		for(int i = 1; i <= tl; ++i) cnt[i] += cnt[i-1];
		for(int i = 0; i < size; ++i) buc[--cnt[pool[i].len]] = i;
	}
	void getBack()
	{
		NODE *p;
		for(int i = size-1; i > 0; --i)
		{
			p = pool+buc[i];
			p->f->right += p->right;
			p->mi = p->f->len+1;
		}
	}
	void solve()
	{
		tupo();
		getBack();
		LL ans = 0;
		NODE *p = root;
		int tl = 0;
		for(char *sp = str; *sp; ++sp)
		{
			int id = hash[*sp];
			if(p->ch[id])
			{
				p = p->ch[id];
				++tl;
			}
			else
			{
				while(p && p->ch[id] == 0) p = p->f;
				if(p)
				{
					tl = p->len+1;
					p = p->ch[id];
				}
				else
				{
					p = root;
					tl = 0;
				}
			}
			if(tl >= K)
			{
				ans += (tl-max(K, p->mi)+1)*p->right;
				++p->cnt;
			}
		}
		for(int i = size-1; pool[buc[i]].len >= K; --i)
		{
			p = pool+buc[i];
			if(p->f->len >= K)
				ans += p->cnt*(p->f->len-max(K, p->f->mi)+1)*p->f->right;
			p->f->cnt += p->cnt;
		}
		printf("%I64d
", ans); } }sam; int main() { int cnt = 0; for(int i = 'a'; i <= 'z'; ++i) hash[i] = cnt++; for(int i = 'A'; i <= 'Z'; ++i) hash[i] = cnt++; while(scanf("%d", &K), K) { scanf("%s", str); sam.init(); for(char *sp = str; *sp; ++sp) sam.extend(hash[*sp]); scanf("%s", str); sam.solve(); } return 0; }

좋은 웹페이지 즐겨찾기