题意

https://nanti.jisuanke.com/t/A1955

求所有本质不同的回文串转成数后的和。

思路

如果了解回文树的构造原理,那么这题就很简单了,回文树每个结点代表一个回文串,每添加一个字符会在两端加上这个字符,我们只需要用res[]数组表示原串的前缀和,然后每添加一个字符的贡献就是res[i]-res[i-len[c]]*10^(len[c]) ,i为这个字符的下标、c为字符,我们累加贡献即可。还是挺简单的,但不晓得当时为什么过的人不是很多。。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = (2e6+5) ;
const int M = 10 ;
const int mod=1000000007;
#define ll long long
ll po[N],res[N];
struct PT
{
int next[N][M] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[N] ;//fail指针,失配后跳转到fail指针指向的节点
int cnt[N] ; //cnt[i]表示i表示的回文字符串在整个字符串中出现了多少次(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
int num[N] ; //num[i]表示i表示的回文字符串中有多少个本质不同的字符串(包括本身)
int len[N] ;//len[i]表示节点i表示的回文串的长度(一个节点表示一个回文串)
int S[N] ;//存放添加的字符
int last ;//指向新添加一个字母后所形成的最长回文串表示的节点。
int n ;//表示添加的字符个数。
int p ;//表示添加的节点个数。
ll ans;
int newnode ( int l ) //新建节点
{
for ( int i = 0 ; i < M ; ++ i ) next[p][i] = 0 ;
cnt[p] = 0 ;
num[p] = 0 ;
len[p] = l ;
return p ++ ;
} void init () //初始化
{
p = 0 ;
newnode ( 0 ) ;//0表示偶数长度串的根
newnode ( -1 ) ;//1表示奇数长度串的根
last = 0 ;
n = 0 ;
S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
fail[0] = 1 ;
ans=0;
} int get_fail ( int x ) //和KMP一样,失配后找一个尽量最长的
{
while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;
return x ;
} void add ( int c,int i)
{
c -= '0' ;
S[++ n] = c ;
int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
if ( !next[cur][c] ) //如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
{
int now = newnode ( len[cur] + 2 ) ;//新建节点
ans=(ans%mod+(res[i]%mod-res[i-len[now]]*po[len[now]]%mod+mod)%mod)%mod;
fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
next[cur][c] = now ;
num[now] = num[fail[now]] + 1 ;
}
last = next[cur][c] ;
cnt[last] ++ ;
} void count ()
{
for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}
} pt ;
char s[N];
int main()
{
cin>>s;
int l=strlen(s);
pt.init();
po[0]=1,res[0]=s[0]-'0';
for(int i=1; i<l; i++)
{
po[i]=po[i-1]*10%mod;
res[i]=(res[i-1]*10%mod+(s[i]-'0'))%mod;
}
for(int i=0; i<l; i++)
{
pt.add(s[i],i);
}
cout<<pt.ans<<endl;
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 南京赛区网络预赛 J.sum

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

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

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

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

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

  5. 计蒜客 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 ...

  6. 计蒜客 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 ...

  7. 计蒜客 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 ...

  8. 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 ...

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

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

随机推荐

  1. Python使用Flask实现RESTful API,使用Postman工具、requests库测试接口

    RESTful是一种API设计规范.在RESTful架构中,主要使用POST,DELETE,PUT和GET四种HTTP请求方式分别对指定的URL资源进行增删改查操作. RESTful之前的做法: /u ...

  2. InfluxDB因修改默认数据目录导致服务无法正常运行的问题(权限问题)

    在实际的生产中,考虑的实际情况,我们会调整一些默认配置,例如,数据目录.InfluxDB修改默认的Data目录后,因权限问题,服务无法正常运行.以下是具体的分析测试过程. 配置文件为 /etc/inf ...

  3. Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.

    在同一个项目中由于flask_sqlalchemy版本不同,有时会报如下错误 错误信息如下: flask_sqlalchemy\__init__.py:: UserWarning: SQLALCHEM ...

  4. Mysql—数据恢复

    根据.frm和.ibd文件恢复表结构和数据

  5. MATLAB实例:非线性曲线拟合

    MATLAB实例:非线性曲线拟合 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 用最小二乘法拟合非线性曲线,给出两种方法:(1)指定非线性函数,(2) ...

  6. LG1345 「USACO5.4」Telecowmunication 最小割

    问题描述 LG1345 题解 点边转化,最小割,完事. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; tem ...

  7. Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心

    B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...

  8. git光速入门

      git的使用和讲解 版本控制 说到版本控制,脑海里总会浮现大学毕业是写毕业论文的场景,你电脑上的毕业论文一定出现过这番景象! 1 2 3 4 5 6 7 8 9 10 11 毕业论文_初稿.doc ...

  9. angular6 multipart/form-data Post

    一般情况下用的都是 第三种  然后碰到后台要求的是这种格式的数据 这时候我们就要修改一下我们的post 请求头,话不多说 直接上代码. 这样子的话 就ok啦!

  10. centos安装nginx并配置SSL证书

    安装nginx的命令 sudo yum install epel-release sudo yum install nginx 让nginx随系统启动而启动 sudo systemctl enable ...