现在的位置: 首页 > 综合 > 正文

str2int hdoj4436

2013年09月06日 ⁄ 综合 ⁄ 共 3270字 ⁄ 字号 评论关闭

由于是处理多个串的子串问题,所以需要先把这些串连接起来,但还要防止连接起来之后子串跨越多个串,所以每个子串之间用一个没有出现的字符分隔,和后缀数组稍有不同,这题的分隔符可以都相同,因为只要转移的过程当中不通过分隔符转移即可。每个节点要额外记录俩个信息,一个是从根节点到达当前节点的子串个数,另一个是从根节点到达当前节点所生成的串前缀和取模,然后按照len从小到大的拓扑序递推即可。做完这题发现了一件蛋疼的事情,那就是发现今天竟然已经20号了,嘛,我们24号就考试了啊,一点还没复习呢,泪奔滚去复习了。


#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(111000);
const int SIGMA_SIZE(11);
const int MAXM(110);
const int MAXE(300010);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const int MOD(2012);
const ULL BASE(31);
const ULL LIM(1000000000000000ull);

inline int idx(char temp)
{
	return temp-'0';
}

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

struct SAM
{
	struct NODE
	{
		int len;
		int count1, count2;  //count1是从根节点到达当前节点的子串个数,count2从根节点到达当前节点所生成的串前缀和取模
		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->count1 = root->count2 = 0;
		memset(root->ch, 0, sizeof(root->ch));
		size = 1;
	}

	NODE *newnode(int tl)
	{
		pool[size].len = tl;
		pool[size].count1 = pool[size].count2 = 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::NODE *que[MAXN << 1];
	int front, back;
	void getMin()   //得到每个状态的min
	{
		front = back = 0;
		que[back++] = root;
		SAM::NODE *p;
		while(front < back)
		{
			p = que[front++];
			for(int i = 0; i < SIGMA_SIZE; ++i)
				if(p->ch[i] && !p->ch[i]->mi)
				{
					p->ch[i]->mi = p->mi+1;
					que[back++] = p->ch[i];
				}
		}
	}
*/
	void turpo()  //按MAX拓扑
	{
		memset(cnt, 0, sizeof(cnt[0])*(last->len+1));
		for(int i = 0; i < size; ++i) ++cnt[pool[i].len];
		for(int i = 1; i <= last->len; ++i) cnt[i] += cnt[i-1];
		for(int i = 0; i < size; ++i) buc[--cnt[pool[i].len]] = i;
	}
/*	void getRight()  //得到每个状态的right
	{
		NODE *tp = root;
		for(char *sp = str; *sp; ++sp)
			tp = tp->ch[idx(*sp)], tp->right = 1;
		for(int i = size-1; i >= 0; --i)
		{
			tp = pool+buc[i];
			if(tp->f)
				tp->f->right += tp->right; 
		}
	}*/
};

SAM sam;

int main()
{
	int n;
	while(~scanf("%d", &n))
	{
		sam.init();
		for(int i = 0; i < n; ++i)
		{
			if(i)
				sam.extend(10);
			scanf("%s", str);
			for(char *sp = str; *sp; ++sp)
				sam.extend(idx(*sp));
		}
		sam.turpo();
		sam.root->count1 = 1;
		for(int i = 1; i < 10; ++i)
			if(sam.root->ch[i])
			{
				sam.root->ch[i]->count1 += sam.root->count1;
				sam.root->ch[i]->count2 = (sam.root->ch[i]->count2+sam.root->count2*10+sam.root->count1*i)%MOD;
			}
		SAM::NODE *p;
		int ans = 0;
		for(int i = 1; i < sam.size; ++i)
		{
			p = sam.pool+buc[i];
			ans = (ans+p->count2)%MOD;
			for(int j = 0; j < 10; ++j)
				if(p->ch[j])
				{
					p->ch[j]->count1 += p->count1;
					p->ch[j]->count2 = (p->ch[j]->count2+p->count2*10+p->count1*j)%MOD;
				}
		}
		printf("%d\n", ans);
	}
	return 0;
}

抱歉!评论已关闭.