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. 华为TaiShan 2280 ARM 服务器

    华为TaiShan 2280 ARM 服务器 华为TaiShan 2280 ARM 服务器  https://e.huawei.com/cn/products/cloud-computing-dc/s ...

  2. session的基本原理及安全性

    1.session原理 提到session,大家肯定会联想到登录,登录成功后记录登录状态,同时标记当前登录用户是谁.功能大体上就是这个样子,但是今天要讲的不是功能,而是实现.通过探讨session的实 ...

  3. 【cf842C】 Ilya And The Tree(dfs、枚举因子)

    C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...

  4. 【 HDU4773 】Problem of Apollonius (圆的反演)

    BUPT2017 wintertraining(15) #5G HDU - 4773 - 2013 Asia Hangzhou Regional Contest problem D 题意 给定两个相离 ...

  5. 【比赛】NOIP2018 旅行

    发现 \(m\) 只有两种取值,于是可做了 树的直接贪心 图的枚举环上的边去掉,然后做树的贪心,搜的时候剪一下枝吧 写得有点乱 #include<bits/stdc++.h> #defin ...

  6. 如何减少UI设计师产品与前端工程师的沟通成本

    在日常工作中,UI设计师/产品与前端工程师难免会有一些冲突,这是我的一些小建议. 1.如何减少时间成本 先制作UI组件,再拼接页面 如果UI给前端的是一堆页面,前端需要花一些时间去整理提取UI组件.另 ...

  7. 做事从来不坚持的我又开始学习PyQt了。。。。。。

    链接附上,不再更新:PyQt5图形界面编程 第一部分 第一个程序 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApp ...

  8. hasattr

    语法: hasattr(object,name) 检测类对象是否具有该name属性或方法

  9. NOIP2018凉凉记

    首先声明一下,我去不了TG,因为我太菜了GD的某教授把我打到PJ了…… 虽然就我这实力普及都不一定1= 不管了,这次是普及,就当扎实基础了.反正HF的 $\text{hhz}$ 也考PJ,见个面多好啊 ...

  10. 数组拆分I

    题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...