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

【题意】

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

【题解】

回文树。
从两个根节点分别遍历整棵回文树。
按照每个节点的定义。
得到每个节点对应的数字就好。
(节点之间都有联系,很容易快速搞出来到达下一个节点的数字是什么的。
有点卡内存。一直优化才没超内存的。
后来发现,空间不用*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;
}

最新文章

  1. 获取Linux主机的CPU、内存、主板、BIOS的信息(Centos)
  2. pip卡住不动的解决方案
  3. Oracle开发常用函数与存储过程
  4. 将正确的 HTTP 头转发给后端服务器的一些问题
  5. 在CentOS 7上安装Python3.5源码包
  6. URL地址下载图片到本地
  7. 使用Qmake在树莓派上开发Opencv程序
  8. PL/SQL Developer远程连接Oracle数据库
  9. Tabs( 选项卡)
  10. 升讯威微信营销系统开发实践:(5) Github 源码:微信接口的 .NET 封装。
  11. 从零开始搭建Jenkins+Docker自动化集成环境
  12. Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台
  13. python全栈开发day24-__new__、__del__、item系列、异常处理
  14. svn分支开发注意事项
  15. HGNC 数据库-人类基因组数据库
  16. VMware虚拟机无法启动,提示“无法打开磁盘,未能锁定文件”
  17. Delphi开发的服务在Windows2003 64位注册方式。
  18. Iptables基础整理
  19. linux无密登录
  20. pom.xml里使用了一系列的版本的框架,配置一个版本属性,让使用版本的都引用这个属性

热门文章

  1. ntp服务及时间同步问题
  2. Android訪问网络,使用HttpURLConnection还是HttpClient?
  3. HDU 5308 I Wanna Become A 24-Point Master
  4. vmware mac 分辨率设置
  5. Linux - 虚拟机中的三种网络连接,桥接、NAT、Host-only详解
  6. 利用python开发的flappy bird 游戏
  7. ORA-03137 - ORA-12592 TNS:BAD PACKET OR ORA-3137 故障处理
  8. JavaScript的并且&amp;&amp;
  9. MySQL 5.6 Reference Manual-14.6 InnoDB Table Management
  10. vue-阻止事件冒泡-开启右键-键盘类事件