HDU 4436 str2int (后缀自动机)
把所有的字符串连接起来,中间用一个未出现的字符分隔开,题意是求这个串的所有子串(中间不含分隔符)形成的数之和。
把这个串加入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 (后缀自动机)的更多相关文章
- str2int HDU - 4436 (后缀自动机)
str2int \[ Time Limit: 3000 ms\quad Memory Limit: 131072 kB \] 题意 给出 \(n\) 个串,求出这 \(n\) 个串所有子串代表的数字的 ...
- 字符串(多串后缀自动机):HDU 4436 str2int
str2int Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
- HDU 4436 str2int
str2int Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 4 ...
- HDU 4622 Reincarnation 后缀自动机
模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
- HDU 6583 Typewriter(后缀自动机)
Typewrite \[ Time Limit: 1500 ms\quad Memory Limit: 262144 kB \] 题意 给出一个字符串 \(s\),现在你需要构造出这个字符串,你每次可 ...
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- Good Article Good sentence HDU - 4416 (后缀自动机)
Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...
- HDU - 6583 Typewriter (后缀自动机+dp)
题目链接 题意:你要打印一段字符串,往尾部添加一个字符需要花费p元,复制一段字符到尾部需要花费q元,求打印完全部字符的最小花费. 一开始想的贪心,后来发现忘了考虑p<q的情况了,还纳闷怎么不对. ...
随机推荐
- 整个trick
数据输入方面:1.image pyramid 图像金字塔.目前代码里是先选取一个scale,然后在每个GPU上按照scale读图片,相应的gt也更改."scales":[440, ...
- Spring注解@Value数值取值转换字符串失败
配置文件(yml)中,配置项如下: cebconfig: INST_CODE: 08801001 SFT_NOTIFY_CEB_CHANNEL: 123456 期望INST_CODE: 0880100 ...
- System.Web.UI
类: System.Web.UI.Page 所以窗体继承的类
- 11_1_GUI
11_1_GUI 1. AWT AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI(Graphics User Interface ...
- 使用session处理用户搜索后数据的上一页和下一页跳转
搜索语句界面: /*单一检索:此处为一个下拉列表的检索*/ if(isset($_POST['submit']) && $_POST['submit'] == '点击搜索') { if ...
- mybatis中oracle转mysql
刚来公司实习,遇到的第一个任务就是这个,简单记录一下思路过程.人菜的很,没啥参考价值. 测试时: 将现有的oracle库转为mysql: 用的Navicat自带数据传输功能,简单粗暴 出现的问题: 1 ...
- django+xadmin在线教育平台(七)
4-3 新建项目 Python2.7 创建虚拟环境. mkvirtualenv mxonline2 安装django pip install django==1.9.8 注意Python2下此处必须用 ...
- give me something new 无用但有趣
屏保系列 http://www.asty.org/cmatrix/dist/cmatrix-1.2a.tar.gz //数码雨 libaa-bin //燃烧 海洋馆 http://search.cp ...
- php数据加密及数据存储和传输
一.前言 个人认为,PHP是世界上为数不多,最人性化的语言. 虽然是二次开发.弱类型语言,由C/C++编写的PHP引擎去解析.但是,其代码优雅性和其运行速度不亚于,其他编译语言. 二.PHP数据加密 ...
- 环形缓冲区实现类(Delphi)
环形缓冲区的用途及原理可以去百度资料狠多的,这里就不介绍了.直接贴代码.代码分别用D7,XE2编译测试 源码下载 http://files.cnblogs.com/lwm8246/uCircleBuf ...