HDU
来源:互联网 发布:windows ssh使用 编辑:程序博客网 时间:2024/06/05 07:50
题目:给你n个由0~9组成的字符串,求不同的子串表示的十进制数的和,对2012取模
思路:将这n个串用10连接起来,建立一个SAM,然后跑拓扑序,注意含有前导0的数不要计算否则会重复计算
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f//0x3f3f3f3fconst int MOD=2012;const int MAXN = 300050, SIZE = 11;struct SAM { int len[MAXN], link[MAXN], next[MAXN][SIZE]; int total, last; inline int newNode(int L) { len[++total] = L; link[total] = 0; for(int i = 0; i < SIZE; ++i) next[total][i] = 0; return total; } inline void Add(int c) { int i, p = last, cur = newNode(len[last] + 1); for(; p && !next[p][c]; p = link[p]) next[p][c] = cur; if(!p) link[cur] = 1;//令其指向初始状态 else { int q = next[p][c]; if(len[q] == len[p] + 1) link[cur] = q; else {//> int clone = newNode(len[p] + 1); for(i = 0; i < SIZE; ++i) next[clone][i] = next[q][i]; link[clone] = link[q]; link[q] = link[cur] = clone;for(; p && next[p][c] == q; p = link[p]) next[p][c] = clone; } } last = cur; } void Init () {//根节点是1 total = 0; last = newNode(0); }}sam;char str[MAXN];int num[MAXN],idx[MAXN];int sum[MAXN],tim[MAXN];int main(){ int n; while(~scanf("%d",&n)){ sam.Init(); while(n--){ scanf("%s",str); for(int i=0;str[i]!='\0';i++) sam.Add(str[i]-'0'); sam.Add(10); } mm(num,0); for(int i=1;i<=sam.total;i++) num[sam.len[i]]++; for(int i=1;i<=sam.total;i++) num[i]+=num[i-1]; for(int i=1;i<=sam.total;i++) idx[num[sam.len[i]]--]=i; mm(sum,0); mm(tim,0); int ans=0; tim[1]=1; for(int i=1;i<=sam.total;i++){ int p=idx[i]; ans=(ans+sum[p])%MOD; for(int j=0;j<10;j++){ if(p==1&&j==0)//去除有前导0的数 continue; if(sam.next[p][j]){ int np=sam.next[p][j]; sum[np]=(sum[np]+10*sum[p]%MOD+tim[p]*j%MOD)%MOD; tim[np]=(tim[np]+tim[p])%MOD; } } } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 公司应用日志收集架构进化过程
- ios 奔溃堆栈写入文件
- 基于阿里云构建可靠懒猪行IT运维平台
- 两款简易计算器纯js版附源码下载
- Codeforces Round #434 div2 A, B , C,D 题 题解
- HDU
- 处理注册时的生日日期格式问题
- 阿里云数据库推出SQL Server 2016 单机基础版本
- Xtrabackup定时备份数据库脚本
- spring boot项目通过外置tomcat运行
- 科三考试---专为denny而写
- Pig Latin 常用指令
- Robot Framework如何对Android的控件定位
- 不惧面试:HTTP协议(1)