把所有的字符串连接起来,中间用一个未出现的字符分隔开,题意是求这个串的所有子串(中间不含分隔符)形成的数之和。

把这个串加入SAM,对所有子串进行拓扑排序,从前往后统计。

记录到达这个节点的路径个数cnt,以及到达这个节点的总和sum。

设父节点是u,子节点是v

sum[v] = sum[u] * 10 + sum[v] + cnt[v]*j;

cnt[v] += cnt[u];

ans就是把所有的sum加起来。

注意:

1.忽略前导零

2.子串中不要含分隔符

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ;
const int segma_size = ;
const int MOD = ; struct Suffix_Automaton
{
int F[MAXN << ]; //fail
int ant; //sum node
int last; //last point
int ch[MAXN << ][segma_size]; //side
int step[MAXN << ]; //len void init()
{
last = ant = ;
memset(F,,sizeof(F));
memset(ch,,sizeof(ch));
memset(step,,sizeof(step));
return;
} void ins( int x )
{
int t = ++ant, pa = last;
step[t] = step[last] + ;
last = t;
for( ; pa && !ch[pa][x]; pa = F[pa] )
ch[pa][x] = t;
if( pa == ) F[t] = ;
else if( step[pa] + == step[ ch[pa][x] ] )
F[t] = ch[pa][x];
else
{
int nq = ++ant, q = ch[pa][x];
memcpy( ch[nq], ch[q], sizeof(ch[nq]) );
step[nq] = step[pa] + ;
F[nq] = F[q];
F[q] = F[t] = nq;
for( ; pa && ch[pa][x] == q; pa = F[pa] )
ch[pa][x] = nq;
}
}
}; int N;
char str[MAXN + ];
int strL;
Suffix_Automaton SAM;
int r[MAXN << ];
int sum[MAXN << ];
int cnt[MAXN << ]; bool cmp( int a, int b )
{
return SAM.step[a] < SAM.step[b];
} void show()
{
for ( int i = ; i <= SAM.ant; ++i )
{
for ( int j = ; j < ; ++j )
{
printf( "%d ", SAM.ch[i][j] );
}
puts("");
}
return;
} int main()
{
//freopen( "s.txt", "w", stdout );
while ( scanf( "%d", &N ) == )
{
SAM.init();
strL = ;
for ( int n = ; n < N; ++n )
{
if ( strL )
str[strL++] = '' + ;
scanf( "%s", &str[strL] );
strL += strlen( &str[strL] );
}
for ( int i = ; i < strL; ++i )
SAM.ins( str[i] - '' ); for ( int i = ; i <= SAM.ant; ++i )
r[i] = i; sort( r + , r + + SAM.ant, cmp );
memset( sum, , sizeof(int)*(SAM.ant+) );
memset( cnt, , sizeof(int)*(SAM.ant+) ); int ans = ;
cnt[] = ;
for ( int i = ; i <= SAM.ant; ++i )
{
int u = r[i];
if ( i > && !cnt[u] ) continue;
ans = ( ans + sum[u] ) % MOD;
for ( int j = ; j < ; ++j )
{
if ( u == && j == ) continue;
if ( !SAM.ch[u][j] ) continue;
int v = SAM.ch[u][j];
sum[v] = ( sum[u] * + sum[v] + cnt[u] * j ) % MOD;
cnt[v] += cnt[u];
}
}
printf( "%d\n", ans ); }
return ;
}

HDU 4436 str2int (后缀自动机)的更多相关文章

  1. str2int HDU - 4436 (后缀自动机)

    str2int \[ Time Limit: 3000 ms\quad Memory Limit: 131072 kB \] 题意 给出 \(n\) 个串,求出这 \(n\) 个串所有子串代表的数字的 ...

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

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

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

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

  4. HDU 4436 str2int

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

  5. HDU 4622 Reincarnation 后缀自动机

    模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...

  6. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  7. HDU 6583 Typewriter(后缀自动机)

    Typewrite \[ Time Limit: 1500 ms\quad Memory Limit: 262144 kB \] 题意 给出一个字符串 \(s\),现在你需要构造出这个字符串,你每次可 ...

  8. Reincarnation HDU - 4622 (后缀自动机)

    Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...

  9. Good Article Good sentence HDU - 4416 (后缀自动机)

    Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...

  10. HDU - 6583 Typewriter (后缀自动机+dp)

    题目链接 题意:你要打印一段字符串,往尾部添加一个字符需要花费p元,复制一段字符到尾部需要花费q元,求打印完全部字符的最小花费. 一开始想的贪心,后来发现忘了考虑p<q的情况了,还纳闷怎么不对. ...

随机推荐

  1. EF 连接数据库 Mysql (database first ) 一个表对应一个模型

    准备工作 1.下载vs2015 2.下载mysql2017 3.安装 1.创建数据库 2. 将数据库映射成模型 3创建aspx 文件. 写下窗体内容的代码 hello_worldEntities en ...

  2. 第46章 DCMI—OV5640摄像头—零死角玩转STM32-F429系列

    第46章     DCMI—OV5640摄像头 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com ...

  3. js将数字转换成中文

           var _change = {            ary0:["零", "一", "二", "三", ...

  4. 太阳地球月亮运行动画(使用@keyframes)

    闲来无事的demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  5. UVA_10139

    The factorial function, n! is defined thus for n a non-negative integer:0! = 1 n! = n×(n−1)! (n > ...

  6. input宽度超出

    设置样式:style=“width:100%”;即可

  7. Linux-日期时间相关命令

    获取当前时间 date [root@VM_0_3_centos ~]# date Mon Mar 18 19:13:33 CST 2019 [root@VM_0_3_centos ~]# date相关 ...

  8. Python学习之购物车

    实现功能: 程序启动,提示用户输入用户名和密码,程序读取余额文件last_salary.txt内容(文件不存在则自动创建),若文件内容为空则提示“首次登录,请输入工资”: 用户可以输入商品编号进行购买 ...

  9. 使用CSS隐藏HTML元素的四种常用方法

    CSS隐藏HTML元素的四种常用方法 1.opacity:设置opacity: 0可以使一个元素变得完全透明. 设置的透明度会被子元素继承,而且无法取消. 通常可以使用opacity属性来制作元素的淡 ...

  10. php-安装与配置-未完待续2

    一,准备工作 在入门指引中,我们已经知道PHP的3个应用领域,不同的场景,需要安装的东西是不同的.具体如下: 服务器端脚本,在通常情况下,需要三样东西:PHP 自身.一个 web 服务器和一个 web ...