str2int

Problem Description
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, and all the possible substrings of each one of them.
It's boring to manipulate strings, so you decide to convert strings in S into integers.
You can convert a string that contains only digits into a decimal integer, for example, you can convert "101" into 101, "01" into 1, et al.
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 get divided by 2012.
Input
There are no more than 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 between 0 and 2011, inclusive, for each test case.
Sample Input
5
101
123
09
000
1234567890
Sample Output
202
Source
 
 
【分析】
  

  广义SAM,每个点记录一个ans,表示它代表的串的值的和。如果有前缀0,记得不能加进去,每个点标记一下它表示的串又做少个是有前缀0的,记为zr,用父亲的ans和zr更新儿子。

  if(now==1&&k==0) t[np].zr=1;
  else t[np].zr+=t[now].zr;

  t[np].ans=(t[np].ans+t[now].ans*10+(t[now].step-t[t[now].pre].step-t[now].zr)*k);
  SAM上有一步是新加一个点,继承某一个点的信息,记得这里要搞一下,把旧点信息减新点信息。

  建完机就可以直接统计答案了哈哈哈!!!

  

  具体看代码:

  

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<set>
using namespace std;
#define Mod 2012
#define Maxn 1000010 int n;
struct node
{
int pre,step,son[];
int ans,zr;
}t[*Maxn];int tot;
int last; char s[Maxn]; void upd(int x)
{
memset(t[x].son,,sizeof(t[x].son));
t[x].pre=;
t[x].ans=;t[x].zr=;
} void add(int now,int k,int np)
{
// if(now==1&&k==0) return;
if(now==&&k==) t[np].zr=;
else /*if(k==0)*/ t[np].zr+=t[now].zr;
t[now].son[k]=np;
t[np].ans=(t[np].ans+t[now].ans*+(t[now].step-t[t[now].pre].step-t[now].zr)*k)%Mod;
} void extend(int k)
{
int np=++tot;upd(tot);
t[np].step=t[last].step+;
int now=last;
while(now&&!t[now].son[k])
{
add(now,k,np);// t[now].son[k]=np;
now=t[now].pre;
}
if(!now) t[np].pre=;
else
{
int p=now,q=t[now].son[k];
if(t[p].step+==t[q].step) t[np].pre=q;
else
{
int nq=++tot;upd(tot);
memcpy(t[nq].son,t[q].son,sizeof(t[nq].son));
t[nq].pre=t[q].pre;
t[q].pre=t[np].pre=nq;
t[nq].step=t[p].step+;
while(now&&t[now].son[k]==q)
{
// t[now].son[k]=nq;
add(now,k,nq);
now=t[now].pre;
}
t[q].ans=(t[q].ans+Mod-t[nq].ans)%Mod;
t[q].zr-=t[nq].zr;
}
}
last=np;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
tot=;
t[++tot].step=;upd(tot);
for(int i=;i<=n;i++)
{
scanf("%s",s+);
int l=strlen(s+);
last=;
for(int j=;j<=l;j++)
{
int k=s[j]-'';
extend(k);
}
}
int ans=;
for(int i=;i<=tot;i++) ans=(ans+t[i].ans)%Mod;
printf("%d\n",ans);
}
return ;
}

[HDU 4436]

2016-09-21 20:51:06

【HDU 4436】 str2int (广义SAM)的更多相关文章

  1. HDU 4436 str2int (后缀自动机SAM,多串建立)

    str2int Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  2. HDU 4436 str2int

    str2int Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 4 ...

  3. HDU 4436 str2int(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4436 [题目大意] 给出一些字符串,由0~9组成,求出所有不同子串的和. [题解] 将所有字符串添 ...

  4. 字符串(多串后缀自动机):HDU 4436 str2int

    str2int Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  5. HDU 4436 str2int (后缀自动机)

    把所有的字符串连接起来,中间用一个未出现的字符分隔开,题意是求这个串的所有子串(中间不含分隔符)形成的数之和. 把这个串加入SAM,对所有子串进行拓扑排序,从前往后统计. 记录到达这个节点的路径个数c ...

  6. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

  7. HDU 4436 (后缀自动机)

    HDU 4436 str2int Problem : 给若干个数字串,询问这些串的所有本质不同的子串转换成数字之后的和. Solution : 首先将所有串丢进一个后缀自动机.由于这道题询问的是不同的 ...

  8. HDU 6405 Make ZYB Happy(广义SAM)

    It's known to all that ZYB is godlike, so obviously he has a large number of titles, such as jskingj ...

  9. 【BZOJ 3926】 [Zjoi2015]诸神眷顾的幻想乡 (广义SAM)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 974  Solved: 573 Descriptio ...

随机推荐

  1. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  2. mysql中 的 ENGINE = innodb; 是什么意思?

    存储引擎是innodb.nnoDB 是 MySQL 上第一个提供外键约束的数据存储引擎,除了提供事务处理外,InnoDB 还支持行锁,提供和 Oracle 一样的一致性的不加锁读取,能增加并发读的用户 ...

  3. 2.redis.3.2 下载,安装、配置、使用 - 2

    上篇简单介绍了 下载,安装,测试,现在直接使用了,看结果 使用的redis服务便是,上篇临时搭建的简易服务,,注意,说的是简易,因为它只是一个单点的“玩具”: 临时在项目登录的时候模拟了一下,这里使用 ...

  4. CWnd::UpdateData

    CWnd::UpdateData 格式: BOOL UpdateData( BOOL bSaveAndValidate = TRUE ); 描述:调用该成员函数初始化在对话框中的数据,或检索和验证对话 ...

  5. Library cache lock 故障解决一例

    今天收到同事电话,说是数据库中一张名为acct_balance进行操作是奇慢,第一反映是不是扫行计划有问题,结果我错了,现将过程记录下来. 用pl/sql连上数据库情况:1.对acct_balance ...

  6. C#创建微信自定义菜单

    string posturl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_to ...

  7. 解决VirtualBox错误:“FATAL:No bootable medium found!”

    VirtualBox错误:“FATAL:No bootable medium found!” 用VirtualBox安装系统出现这个错误的几率极高,因为当哥出现同样问题的时候股沟了下”FATAL:No ...

  8. Cogs 1844. [JSOI2008]最大数maxnumber

    [JSOI2008]最大数maxnumber ★★ 输入文件:bzoj_1012.in 输出文件:bzoj_1012.out 简单对比 时间限制:3 s 内存限制:162 MB [题目描述] 现在请求 ...

  9. TDirectory.GetFiles获取指定目录下的文件

    使用函数: System.IOUtils.TDirectory.GetFiles 所有重载: class function GetFiles(const Path: string): TStringD ...

  10. C#中的反射 Assembly.Load() Assembly.LoadFrom()

    一些关于C#反射的知识,估计也就最多达到使用API的程度,至于要深入了解,以现在的水平估计很难做到,所以下面此篇文章,以作为一个阶段的总结. 对于反射的总结,我想从以下几个方面展开,首先是反射程序集, ...