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

题意

给一串由0..9组成的数字字符串,求所有不同回文串的权值和。比如说“1121”这个串中有“1”,“2”,“11”,“121”三种回文串,他们的权值分别是1,2,11,121。最终输出ans=135。

分析

第一次知道马拉车是manacher。。。涨姿势了

在马拉车进行的过程中,进行子回文串的统计去重。

这里的哈希去重方法重点学习理解。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
//#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
//const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6 + ;
const int maxm = +;
const int mod = ; ull base=;
ull p[maxn<<],has[maxn<<];
ll pw[maxn<<],sum[maxn<<];
const int MOD=;
int head[maxn<<],nxt[maxn<<],cnt=;
ull val[maxn];
bool exist(ull now){
int u=now%MOD;
for(int i=head[u];i;i=nxt[i]){
if(val[i]==now) return true;
}
val[cnt]=now;
nxt[cnt]=head[u];
head[u]=cnt++;
return false;
}
ull gethas(int l,int r){
return has[r]-has[l-]*p[r-l+];
}
ll solve(int l,int r){
ull tmp=gethas(l,r);
if(exist(tmp)) return ;
ll ans=(sum[r]-sum[l-]*pw[(r-l++)/]%mod+mod)%mod;
return ans;
}
char s[maxn];
char Ma[maxn<<];
int Mp[maxn<<];
ll Manacher(char s[],int len){
int l=;
Ma[l++]='$';
Ma[l++]='#';
for(int i=;i<len;i++){
Ma[l++]=s[i];
Ma[l++]='#';
}
Ma[l]=;
pw[]=p[]=;
has[]=sum[]=;
for(int i=;i<=l;i++){
p[i]=p[i-]*base;
has[i]=has[i-]*base+Ma[i];
pw[i]=pw[i-]*%mod;
if(Ma[i]>=''&&Ma[i]<=''){
sum[i]=(sum[i-]*+Ma[i]-'')%mod;
}else{
sum[i]=sum[i-];
}
}
ll ans=;
int mx=,id=;
for(int i=;i<l;i++){
if(Ma[i]!='#') ans=(ans+solve(i,i))%mod;
Mp[i]=mx>i?min(Mp[*id-i],mx-i):;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]]){
if(Ma[i+Mp[i]]!='#') ans=(ans+solve(i-Mp[i],i+Mp[i]))%mod;
Mp[i]++;
}
if(mx<i+Mp[i]){
mx=i+Mp[i];
id=i;
}
} return ans;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("input.txt", "w", stdout);
#endif
scanf("%s",s);
int len=strlen(s);
printf("%lld\n",Manacher(s,len));
return ;
}

回文树的做法是先构建一颗回文树,然后dfs奇偶节点,当前节点的所代表的数字=当前添加的数字*pow(10,当前回文串长度-1) +他父亲的数字*10+当前添加的数字。

比如:33->1331  就是1*1000+33+1。此外有点卡空间,注意内存使用

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
//#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
//const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6 + ;
const int maxm = +;
const int mod = 1e9+; struct PAM{
int nxt[maxn][];
int fail[maxn];
int cnt[maxn];
int num[maxn];
int len[maxn];
int s[maxn];
int last,n,p; int newnode(int w){
for(int i=;i<;i++) nxt[p][i]=;
num[p]=cnt[p]=;
len[p]=w;
return p++;
}
void init(){
n=last=p=;
newnode();
newnode(-);
s[n]=-;
fail[]=;
}
int get_fail(int x){
while(s[n-len[x]-]!=s[n]) x=fail[x];
return x;
}
void add(int c){
c-='';
s[++n]=c;
int cur=get_fail(last);
if(!nxt[cur][c]){
int now=newnode(len[cur]+);
fail[now]=nxt[get_fail(fail[cur])][c];
nxt[cur][c]=now;
num[now]=num[fail[now]]+;
}
last=nxt[cur][c];
cnt[last]++;
}
void Count(){
for(int i=p-;i>=;i--) cnt[fail[i]]+=cnt[i];
}
};
PAM pam;
char s[maxn];
ll odd=;
ll even=;
ll qpow(ll a,ll b){
ll res=;
while(b){
if(b&) res=a*res%mod;
b>>=;
a=a*a%mod;
}
return res;
}
void dfs_odd(int x,ll fa){
for(int i=;i<=;i++){
if(pam.nxt[x][i]){
ll cur;
if(pam.len[pam.nxt[x][i]]==){
odd=(i+odd)%mod;
cur=i;
}else{
cur=(i*qpow(,pam.len[pam.nxt[x][i]]-)%mod+i+fa*%mod)%mod;
odd=(odd+cur%mod)%mod;
}
dfs_odd(pam.nxt[x][i],cur);
}
}
}
void dfs_even(int x,ll fa){
for(int i=;i<=;i++){
if(pam.nxt[x][i]){
ll cur=(i*qpow(,pam.len[pam.nxt[x][i]]-)%mod+i+fa*%mod)%mod;
even=(even+cur)%mod;
dfs_even(pam.nxt[x][i],cur);
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("input.txt", "w", stdout);
#endif
pam.init();
scanf("%s",s);
int len=strlen(s);
for(int i=;i<len;i++) pam.add(s[i]);
dfs_odd(,);
dfs_even(,);
printf("%lld\n",(odd+even)%mod); return ;
}

ACM-ICPC 2018 南京赛区网络预赛 I Skr (马拉车+hash去重)或(回文树)的更多相关文章

  1. ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)

    https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了. ...

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

随机推荐

  1. 【拓扑排序】烦人的幻灯片(slides)

    1395:烦人的幻灯片(slides) 时间限制: 1000 ms         内存限制: 65536 KB提交数: 753     通过数: 416 [题目描述] 李教授将于今天下午作一次非常重 ...

  2. CF1139E Maximize Mex(二分图匹配,匈牙利算法)

    好题.不过之前做过的[SCOI2010]连续攻击游戏跟这题一个套路,我怎么没想到…… 题目链接:CF原网 洛谷 题目大意:在一个学校有 $n$ 个学生和 $m$ 个社团,每个学生有一个非负整数能力值 ...

  3. Python反转过程学习

    Pyhon的反转过程的学习: #coding:utf-8 #反转过程.py """ def add(x,y): return x+y params=(1,2) add(* ...

  4. nodejs的某些api~(五) HTTP模块

    HTTP的模块是nodejs最重要的模块(应该是),最近在看HTTP权威指南,重新过了一遍http协议和web客户端.再来看这个http. HTTP构建于TCP之上,属于应用层协议,继承自tcp服务器 ...

  5. Educational Codeforces Round 46 C - Covered Points Count

    C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...

  6. J2EE--常见面试题总结 -- ( 一)

    StringBuilder和StringBuffer的区别: String       字符串常量   不可变  使用字符串拼接时是不同的2个空间 StringBuffer  字符串变量   可变   ...

  7. Electron一学习资源收集和练习demo

    1.近日为了做项目查资料学习electron,简直头都要炸了,就官方的electron-quick-start的例子进行了基本的练习之后,不断的查资料终于发现一些有用的demo来看源代码学习,一遍看代 ...

  8. 【洛谷P1403】约数研究

    题目大意:求\[\sum\limits_{i=1}^n\sum\limits_{d|i}1\] 题解:交换求和顺序即可. \[\sum\limits_{i=1}^n\sum\limits_{d|i}1 ...

  9. C# http请求带请求头部分

    直接上代码 前台调用: <script type="text/javascript"> function zLoginCheck() { var Account = ' ...

  10. (转)CDN的作用与基本过程

    背景:积累大型网站开发中需要掌握的技术. CDN的作用与基本过程 https://blog.csdn.net/lihao21/article/details/52808747#comments CDN ...