str2int

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 841    Accepted Submission(s): 297

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
 
Recommend
zhoujiaqi2010
 

使用SAM进行多串建立。

然后拓扑排序,

之后累加计数就可以了。

注意前导0的要去掉。

/* ***********************************************
Author :kuangbin
Created Time :2013-10-11 17:50:31
File Name :E:\2013ACM\专题强化训练\区域赛\2012天津\F.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MOD = ;
const int CHAR = ;
const int MAXN = ;
struct SAM_Node
{
SAM_Node *fa, *next[CHAR];
int len;
int id,pos;
int cnt;
int sum;
SAM_Node(){}
SAM_Node(int _len)
{
fa = ;
len = _len;
memset(next,,sizeof(next));
cnt = sum = ;
}
};
SAM_Node SAM_node[MAXN*], *SAM_root, *SAM_last;
int SAM_size;
SAM_Node *newSAM_Node(int len)
{
SAM_node[SAM_size] = SAM_Node(len);
SAM_node[SAM_size].id = SAM_size;
return &SAM_node[SAM_size++];
}
SAM_Node *newSAM_Node(SAM_Node *p)
{
SAM_node[SAM_size] = *p;
SAM_node[SAM_size].id = SAM_size;
SAM_node[SAM_size].cnt = SAM_node[SAM_size].sum = ;
return &SAM_node[SAM_size++];
} void SAM_init()
{
SAM_size = ;
SAM_root = SAM_last = newSAM_Node();
SAM_node[].pos = ;
}
void SAM_add(int x,int len)
{
SAM_Node *p = SAM_last, *np = newSAM_Node(p->len+);
np->pos = len;
SAM_last = np;
for(;p && !p->next[x];p = p->fa)
p->next[x] = np;
if(!p)
{
np->fa = SAM_root;
return;
}
SAM_Node *q = p->next[x];
if(q->len == p->len + )
{
np->fa = q;
return;
}
SAM_Node *nq = newSAM_Node(q);
nq->len = p->len + ;
q->fa = nq;
np->fa = nq;
for(;p && p->next[x] == q;p = p->fa)
p->next[x] = nq;
}
//多串的建立,注意SAM_init()的调用
void SAM_build(char *s)
{
int len = strlen(s);
SAM_last = SAM_root;
for(int i = ;i < len;i++)
{
if( !SAM_last->next[s[i] - ''] || !(SAM_last->next[s[i] - '']->len == i+) )
SAM_add(s[i] - '',i+);
else SAM_last = SAM_last->next[s[i] - ''];
}
} char str[MAXN];
int topocnt[MAXN];
SAM_Node *topsam[MAXN*];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while(scanf("%d",&n) == )
{
SAM_init();
for(int i = ;i < n;i++)
{
scanf("%s",str);
SAM_build(str);
}
//continue;
memset(topocnt,,sizeof(topocnt));
for(int i = ;i < SAM_size;i++)
topocnt[SAM_node[i].len]++;
for(int i = ;i < MAXN;i++)
topocnt[i] += topocnt[i-];
for(int i = ;i < SAM_size;i++)
topsam[--topocnt[SAM_node[i].len]] = &SAM_node[i];
int ans = ;
SAM_root->cnt = ;
for(int i = ;i < SAM_size;i++)
{
SAM_Node *tmp = topsam[i];
for(int j = ;j < ;j++)
{
if(i == && j == )continue;
if(tmp->next[j])
{
SAM_Node *q = tmp->next[j];
q->cnt = (q->cnt + tmp->cnt)%MOD;
q->sum = (q->sum + tmp->sum*+tmp->cnt*j)%MOD;
}
}
ans = (ans + tmp->sum)%MOD;
}
printf("%d\n",ans); }
return ;
}

HDU 4436 str2int (后缀自动机SAM,多串建立)的更多相关文章

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

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

  2. [hdu4436 str2int]后缀自动机SAM(或后缀数组SA)

    题意:给n个数字串,求它们的所有不包含前导0的不同子串的值之和 思路:把数字串拼接在一起,构造SAM,然后以每个状态的长度len作为特征值从小到大排序,从前往后处理每个状态,相当于按拓扑序在图上合并计 ...

  3. [转]后缀自动机(SAM)

    原文地址:http://blog.sina.com.cn/s/blog_8fcd775901019mi4.html 感觉自己看这个终于觉得能看懂了!也能感受到后缀自动机究竟是一种怎样进行的数据结构了. ...

  4. 后缀自动机(SAM)奶妈式教程

    后缀自动机(SAM) 为了方便,我们做出如下约定: "后缀自动机" (Suffix Automaton) 在后文中简称为 SAM . 记 \(|S|\) 为字符串 \(S\) 的长 ...

  5. 后缀自动机(SAM)速成手册!

    正好写这个博客和我的某个别的需求重合了...我就来讲一讲SAM啦qwq 后缀自动机,也就是SAM,是一种极其有用的处理字符串的数据结构,可以用于处理几乎任何有关于子串的问题,但以学起来异常困难著称(在 ...

  6. 后缀自动机SAM学习笔记

    前言(2019.1.6) 已经是二周目了呢... 之前还是有一些东西没有理解到位 重新写一下吧 后缀自动机的一些基本概念 参考资料和例子 from hihocoder DZYO神仙翻译的神仙论文 简而 ...

  7. 【算法】后缀自动机(SAM) 初探

    [自动机] 有限状态自动机的功能是识别字符串,自动机A能识别字符串S,就记为$A(S)$=true,否则$A(S)$=false. 自动机由$alpha$(字符集),$state$(状态集合),$in ...

  8. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

  9. 浅谈后缀自动机SAM

    一下是蒟蒻的个人想法,并不很严谨,仅供参考,如有缺误,敬请提出 参考资料: 陈立杰原版课件 litble 某大神 某大神 其实课件讲得最详实了 有限状态自动机 我们要学后缀自动机,我们先来了解一下自动 ...

随机推荐

  1. Integer中1000==1000为false而100==100为true

    查看Integer.java类,会发现有一个内部私有类,IntegerCache.java,它缓存了从-128到127之间的所有的整数对象.如果在这个区间内,他就会把变量当做一个变量,放到内存中:但如 ...

  2. Python 入门基础7 --文件操作

    今日目录: 一.文件处理 1.什么是文件 2.为何用文件 3.如何用文件 4.文件操作 5.常用方法 6.文件内指针的移动 7.with的使用 一.文件处理 1. 什么是文件 文件是操作系统为用户/应 ...

  3. mybatis介绍——(一)

    官方API:http://www.mybatis.org/mybatis-3/index.html 中文: http://www.mybatis.org/mybatis-3/zh/index.html ...

  4. PyTorch学习系列(九)——参数_初始化

    from:http://blog.csdn.net/VictoriaW/article/details/72872036 之前我学习了神经网络中权值初始化的方法 那么如何在pytorch里实现呢. P ...

  5. 【ARTS】01_01_左耳听风-20181112~1116

    每周至少做一个 leetcode 的算法题.阅读并点评至少一篇英文技术文章.学习至少一个技术技巧.分享一篇有观点和思考的技术文章.(也就是 Algorithm.Review.Tip.Share 简称A ...

  6. USB协通讯议--深入理解【转】

    转自:http://blog.csdn.net/myarrow/article/details/8484113 0. 基本概念 一个[传输](控制.批量.中断.等时):由多个[事务]组成: 一个[事务 ...

  7. asp.net防SQL/JS注入攻击:过滤标记

    /// <summary>/// 过滤标记/// </summary>/// <param name="NoHTML">包括HTML,脚本,数据 ...

  8. TcxGrid 去除<No data to display>

  9. laravel项目利用twemproxy部署redis集群的完整步骤

    Twemproxy是一个代理服务器,可以通过它减少Memcached或Redis服务器所打开的连接数.下面这篇文章主要给大家介绍了关于laravel项目利用twemproxy部署redis集群的相关资 ...

  10. 【LOJ】#2108. 「JLOI2015」装备购买

    题解 换成long double才过--出题人丧心病狂卡精度 只要按照费用排序从小到大排序,一个个插入线性基,插入的时候加上费用即可 代码 #include <bits/stdc++.h> ...