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. 一些CSS3的乐趣 - 工作也能发现乐的源头

    中秋节 translate 前些日子做一个中秋节的专题,主要就是写一个效果,月亮滚动,花瓣飘落.具体代码如下: .icons {z-index:10088; position:absolute; -w ...

  2. Codeforces 237 div2 B. Marathon(关于精度损失的教训)

    题目链接:http://codeforces.com/contest/404/problem/B?csrf_token=6292hf3e1h4g5e0d16a996ge6bgcg7g2 解题报告:一个 ...

  3. centos7,php7 安装mysqli扩展

    首先安装MySQL https://www.cnblogs.com/manzb/p/9560403.html   php7安装后没有安装mysqli扩展的话: 安装mysqli扩展 1.到php文件e ...

  4. java交互方式中的同步与异步

    JAVA中交互方式分为同步和异步两种: 1.同步交互:指发送一个请求,需要等待返回,然后才能够发送下一个请求,有个等待过程; 2.异步交互:指发送一个请求,不需要等待返回,随时可以再发送下一个请求,即 ...

  5. How to become a successful bug bounty hunter

    出处:https://www.hackerone.com/blog/become-a-successful-bug-bounty-hunter 如果你梦想成为赏金猎人,你的梦想就会成真 - 不要把你的 ...

  6. sort命令的k选项大讨论【转】

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  7. C#排队处理DEMO

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 不能访问本地服务器场。没有注册带有FeatureDependencyId 的 Cmdlet

      不能访问本地服务器场.没有注册带有FeatureDependencyId 的 Cmdlet. 原因: 我有两个域管理员账号,分别:sp\administrator,sp\spadmin 其中后者才 ...

  9. 使用maven命令终端构建一个web项目及发布该项目

    构建环境: maven版本:3.3.9 系统平台:Windows7 x64 JDK版本:1.7 构建步骤: 1.打开maven安装目录,在地址栏输入cmd进入命令窗口 2.输入命令mvn archet ...

  10. SpringMVC介绍及参数绑定

    本节内容: SpringMVC介绍 入门程序 SpringMVC架构 SpringMVC整合MyBatis 参数绑定 SpringMVC和Struts2的区别 一.SpringMVC介绍 1. 什么是 ...