【链接】 我是链接,点我呀:)

【题意】

让你把一个字符串的所有回文子串加起来。(当做数字加起来)
求他们的和。

【题解】

回文树。
从两个根节点分别遍历整棵回文树。
按照每个节点的定义。
得到每个节点对应的数字就好。
(节点之间都有联系,很容易快速搞出来到达下一个节点的数字是什么的。
有点卡内存。一直优化才没超内存的。
后来发现,空间不用*2的。。。
$直接O(N*ALP)的空间复杂度就好$
$不用O(N*2*ALP)。。$

【代码】

#include <bits/stdc++.h>
#define LL long long
using namespace std; const int maxn = 2e6+10;
const int ALP = 10;
const LL MOD = 1e9+7; LL _pow(LL x,LL y){
LL a = 1;x%=MOD;
while (y){
if (y&1) a = (a*x)%MOD;
x=(x*x)%MOD;
y>>=1;
}
return a;
} struct PAM{
int nex[maxn][ALP];
int fail[maxn];
int len[maxn],root,root1;
int s[maxn];
int last,n,p;
LL ans; int newnode(int l){
for(int i=0;i<ALP;i++)
nex[p][i]=0;
len[p]=l;
return p++;
}
void init(){
ans = 0;
p = 0;
root = newnode(0);
root1 = newnode(-1);
last = 0;
n = 0;
s[n] = -1;
fail[0] = 1;
}
int get_fail(int x){
while(s[n-len[x]-1] != s[n]) x = fail[x];
return x;
}
void add(int c){
c = c-'0';
s[++n] = c;
int cur = get_fail(last);
if(!nex[cur][c]){
int now = newnode(len[cur]+2);
fail[now] = nex[get_fail(fail[cur])][c];
nex[cur][c] = now;
}
last = nex[cur][c];
} void dfs(int x,LL s,int len){
for (int i = 0;i < 10;i++){
if (nex[x][i]!=0){
LL ts = s;
int tlen = len;
if (x==root1){
ts = (ts*10 + i)%MOD;
tlen++;
}else if (x==root){
ts = (ts*10 + i)%MOD;
ts = (ts*10 + i)%MOD;
tlen+=2;
}else{
ts = (ts*10 + i)%MOD;
tlen++;
ts = (ts+i*_pow(10,tlen)%MOD)%MOD;
tlen++;
}
ans = (ans+ts)%MOD;
dfs(nex[x][i],ts,tlen);
}
}
}
}pam; string s;
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
cin >> s;
int len = s.size();
pam.init();
for(int i=0;i<len;i++){
pam.add(s[i]);
}
pam.dfs(pam.root1,0,0);
pam.dfs(pam.root,0,0);
printf("%lld\n",pam.ans);
return 0;
}

【ACM-ICPC 2018 南京赛区网络预赛 I】Skr的更多相关文章

  1. ACM-ICPC 2018 南京赛区网络预赛 I Skr (马拉车+hash去重)或(回文树)

    https://nanti.jisuanke.com/t/30998 题意 给一串由0..9组成的数字字符串,求所有不同回文串的权值和.比如说“1121”这个串中有“1”,“2”,“11”,“121” ...

  2. ACM-ICPC 2018 南京赛区网络预赛 I. Skr(回文树)

    题意 https://nanti.jisuanke.com/t/A1955 求所有本质不同的回文串转成数后的和. 思路 如果了解回文树的构造原理,那么这题就很简单了,回文树每个结点代表一个回文串,每添 ...

  3. ACM-ICPC 2018 南京赛区网络预赛 J.sum

    A square-free integer is an integer which is indivisible by any square number except 11. For example ...

  4. ACM-ICPC 2018 南京赛区网络预赛 E题

    ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...

  5. ACM-ICPC 2018 南京赛区网络预赛B

    题目链接:https://nanti.jisuanke.com/t/30991 Feeling hungry, a cute hamster decides to order some take-aw ...

  6. 计蒜客 30999.Sum-筛无平方因数的数 (ACM-ICPC 2018 南京赛区网络预赛 J)

    J. Sum 26.87% 1000ms 512000K   A square-free integer is an integer which is indivisible by any squar ...

  7. 计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)

    G. Lpl and Energy-saving Lamps 42.07% 1000ms 65536K   During tea-drinking, princess, amongst other t ...

  8. 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)

    A. An Olympian Math Problem 54.28% 1000ms 65536K   Alice, a student of grade 66, is thinking about a ...

  9. ACM-ICPC 2018 南京赛区网络预赛 B. The writing on the wall

    题目链接:https://nanti.jisuanke.com/t/30991 2000ms 262144K   Feeling hungry, a cute hamster decides to o ...

  10. ACM-ICPC 2018 南京赛区网络预赛

    轻轻松松也能拿到区域赛名额,CCPC真的好难 An Olympian Math Problem 问答 只看题面 54.76% 1000ms 65536K   Alice, a student of g ...

随机推荐

  1. Objective-C学习笔记(二十二)——初始化方法init的重写与自己定义

    初学OC.对init这种方法不是非常了解.我们如今来分别对init方法进行重写以及自己定义,来加深对他的了解. 本样例也是用Person类来进行測试. (一)重写init方法. (1)在Person. ...

  2. solr实战-(一)

    实现用户数据索引及查询 1. 启动solr       solr start 2. 创建collection       solr create -c user 3. schema中加入field   ...

  3. QT官方下载地址

    http://download.qt.io/official_releases watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/ ...

  4. ubuntu清华源【转】

    https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 可以选择ubuntu的版本更新源.

  5. SQLite 常用函数

    SQLite 常用函数 参考: SQLite 常用函数 | 菜鸟教程http://www.runoob.com/sqlite/sqlite-functions.html SQLite 常用函数 SQL ...

  6. 2014.9.20Hashtable概述

    hashtable叫哈希表,用于表示键值的集合,这些键值对根据键的哈希代码进行组织,其每个元素都存储于DictionaryEntry对象中的键值对.键不能为空引用. count:获取包含在hashta ...

  7. android 学习记录-----------android 活动 意图 碎片

    将此篇博客作为记录android项目开发过程中的学习记录

  8. ADO.NET增删改

    static void Main1(string[] args) {添加造连接字符串string connstring = "server=.;database=mydb;user=sa;p ...

  9. 09.javaweb简单标签编程

    一.简单标签 1,  简介:由于传统标签使用三个标签接口来完成不同的功能,显得过于繁琐,不利于标签技术的推广, SUN公司为降低标签技术的学习难度,在JSP 2.0中定义了一个更为简单.便于编写和调用 ...

  10. hdu1829 A Bug's Life 基础种类并查集

    题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...