HDU 4436 str2int

ProblemDescription

In this problem,you are given several strings that contain only digits from '0' to '9',inclusive.
An example is shown below.
101
123
The set S of strings is consists of the N strings given in the input file, andall the possible substrings of each one of them.
It's boring to manipulate strings, so you decide to convert strings in S intointegers.
You can convert a string that contains only digits into a decimal integer, forexample, you can convert "101" into 101, "01" into 1, etal.
If an integer occurs multiple times, you only keep one of them.
 
For example, in the example shown above, all the integers are 1, 10, 101, 2, 3,12, 23, 123.
Your task is to calculate the remainder of the sum of all the integers you getdivided by 2012.

 

 

Input

There are no morethan 20 test cases.
The test case starts by a line contains an positive integer N.
Next N lines each contains a string consists of one or more digits.
It's guaranteed that 1≤N≤10000 and the sum of the length of all the strings≤100000.
The input is terminated by EOF.

 

 

Output

An integer between0 and 2011, inclusive, for each test case.

 

 

Sample Input

5

101

123

09

000

1234567890

 

 

Sample Output

202

题目大意:给定n个数字串,求出所有字符串的不同子串(前置0没有用)的和对2012取模的结果。

方法:将所有字符串连接起来,中间用10隔开。然后构造后缀自动机,对于构造的自动机,遍历子串的时候,root0不能走,因为前置0没有用,否则子串就会被重复计算。同时关于10路都不能走,因为不同子串之间不能形成子串。这样就构造好了。然后就是遍历所有子串算出和取模的过程了

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<deque>
#include<algorithm>
using namespace std;

struct State
{
	State *pre ,*go[15];//pre是上一个可以接收后缀的结点
	int val ,cnt ,sum;
};

State * root ,* tail ,que[400010] ,*sa[210010];
int n ,q ,tot ,sum ,len;
char ch[100010];
int rank1[400010] ;

void add(int w ,int len)
{
	State *p = tail ,*np = &que[tot++];
	np->val = len;
	//找到上一个能接受后缀的节点
	while(p && p->go[w]==NULL)
	{
		p->go[w] = np;
		p = p->pre;
	}
	//如果没有找到这样的节点,就将pre指针指向root
	if(p==NULL)
	{
		np->pre = root;
	}
	//如果找到这样的节点,就把这个节点pre就指向这个节点对应的儿子
	else
	{
		State *q = p->go[w];
		if(p->val + 1==q->val)
		{
			np->pre = q;
		}
		else
		{
			State *nq = &que[tot++];
			//将q的指针赋值给nq,将nq的pre指向p,将np和q的pre指向nq
			*nq = *q;
			nq->val = p->val + 1;
			q->pre = np->pre = nq;
			//同时将p的儿子为w且指向q的全部指向nq
			while(p && p->go[w]==q)
			{
				p->go[w] = nq;
				p = p->pre;
			}
		}
	}
	tail = np;
}

void solve()
{
	memset(rank1,0,sizeof(rank1));
    sum = 0;
    //按照到root的步数进行排序
    for(int i = 0;i < tot;i++)
	{
		rank1[que[i].val]++;
	}
	for(int i = 1;i < len;i++)
	{
		rank1[i] += rank1[i-1];
	}
	for(int i = 0;i < tot;i++)
	{
		sa[--rank1[que[i].val]] = &que[i];
	}
	
	root->cnt = 1;
	root->sum = 0;
	State *p ,*q;
	for(int i = 0;i < tot;i++)
	{
		p = sa[i];
		for(int j = 0;j < 10;j++)
		{
			if(i==0 && j==0)
			{
				continue;
			}
			if(p->go[j]!=NULL)
			{
				q = p->go[j];
				q->cnt = (p->cnt + q->cnt) % 2012;
				q->sum = (q->sum + p->cnt * j + p->sum * 10) % 2012;
			}
		}
		sum = (sum + p->sum) % 2012;
	}
}

int main()
{
	while(~scanf("%d",&n))
	{
		tot = 0;
		len = 1;
		root = tail = &que[tot++];
		for(int i = 0;i < n;i++)
		{
			scanf("%s",ch);
			for(int j = 0;ch[j]!='\0';j++)
			{
				add(ch[j] - '0',len++);
			}
			add(10,len++);
		}
		solve();
		printf("%d\n",sum);
		for(int i = 0;i < tot;i++)
		{
			que[i].cnt = que[i].sum = 0;
			que[i].pre = NULL;
			memset(que[i].go,NULL,sizeof(que[i].go));
		}
	}
	return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值