Interesting (manacher + 前缀和处理)
题意:相邻的两端回文串的价值为两个回文串总的区间左端点 × 区间右端点。然后计算目标串中所有该情况的总和。
思路:首先用manacher求出所有中心点的最大半径,然后我们知道对于左区间我们把贡献记录在右端点,右区间记录在左端点。然后细节的我就不太懂了。迷~
#include<bits/stdc++.h>
using namespace std; const int maxn = 2e6 +;
const int mod = 1e9 + ;
long long rad[maxn], L[maxn], R[maxn], cnt1[maxn], cnt2[maxn];
char in[maxn], str[maxn]; void manacher(){
int len = strlen(in), l = ;
str[l ++] = '$'; str[l ++] = '#';
for(int i = ; i < len; i ++)
str[l ++] = in[i], str[l ++] = '#';
str[l] = ;
int mx = , id = ;
for(int i = ; i < l; i ++) {
rad[i] = mx > i ? min(rad[ * id - i], 1LL * mx - i) : ;
while(str[i + rad[i]] == str[i - rad[i]]) rad[i] ++;
if(i + rad[i] > mx){
mx = i + rad[i];
id = i;
}
}
} int main(){ while(~scanf("%s", in)) {
int len = strlen(in);
manacher();
// for(int i = 0; i < 2 * len + 2; i ++) printf(" %d ", rad[i]);
memset(cnt1, , sizeof(cnt1));
memset(cnt2, , sizeof(cnt2));
for(int i = ; i <= * len; i ++){
cnt1[i - rad[i] + ] += i; cnt1[i + ] -= i;
cnt2[i - rad[i] + ] ++; cnt2[i + ] --;
}
for(int i = ; i <= * len; i ++){
cnt1[i] += cnt1[i - ], cnt2[i] += cnt2[i - ];
if(i % == ) R[i/] = (cnt1[i] - i / * cnt2[i]) % mod;
}
memset(cnt1, , sizeof(cnt1));
memset(cnt2, , sizeof(cnt2));
for(int i = ; i <= * len; i ++) {
cnt1[i + rad[i]] -= i; cnt1[i] += i;
cnt2[i + rad[i]] --; cnt2[i] ++;
}
for(int i = ; i <= * len; i ++) {
cnt1[i] += cnt1[i - ]; cnt2[i] += cnt2[i - ];
if(i % == ) L[i / ] = (cnt1[i] - i / * cnt2[i]) % mod;
}
long long ans = ;
for(int i = ; i < len; i ++) ans = (ans + L[i] * R[i + ] % mod) % mod;
printf("%lld\n", ans);
}
return ;
}
Interesting (manacher + 前缀和处理)的更多相关文章
- 多校1005 HDU5785 Interesting (manacher)
// 多校1005 HDU5785 Interesting // 题意:给你一个串,求相邻两个回文串左边端点*右边端点的和 // 思路:马拉车算出最长回文半径,求一个前缀和,既得到每个点对答案的贡献. ...
- 【HDU5785】Interesting [Manacher]
Interesting Time Limit: 30 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description Input Outp ...
- HDU-3613-Best Reward(Manacher, 前缀和)
链接: https://vjudge.net/problem/HDU-3613 题意: After an uphill battle, General Li won a great victory. ...
- HDU 5785 Interesting manacher + 延迟标记
题意:给你一个串,若里面有两个相邻的没有交集的回文串的话,设为S[i...j] 和 S[j+1...k],对答案的贡献是i*k,就是左端点的值乘上右端点的值. 首先,如果s[x1....j].s[x2 ...
- cf519D. A and B and Interesting Substrings(前缀和)
题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...
- 【SAM manacher 倍增】bzoj3676: [Apio2014]回文串
做法一:PAM:做法二:SAM+manacher.前缀树上倍增 Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你 ...
- Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP
题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...
- HDU5785 Interesting(Manacher + 延迟标记)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5785 Description Alice get a string S. She think ...
- hdu3613 Best Reward manacher+贪心+前缀和
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- windows 端口被占用,并杀死进程的方法
netstat -ano | findstr 8081 查询端口 被什么进程占用 tasklist | findstr 2184 根据进程号 查询任务名称 taskkill /f /t /im jav ...
- google的Python风格规范
Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. ...
- Sqlalchemy model 文件自动生成
自动生成Sqlalchemy的models文件的包早用过了,有个字段类型做了改动,调了得10几分钟才搞定.记录下自动生成models文件的python包sqlacodegen sqlacodegen已 ...
- 安装redis时Newer version of jemalloc required错误解决
问题: [root@localhost redis-4.0.0]# make cd src && make allmake[1]: Entering directory `/root/ ...
- selenium 操作过程中,元素标红高亮的两种实现方式
在使用selenium时,动作元素标红高亮,在定位问题时相当好用,有以下二种方法可以实现 一.使用js将元素属性修改 这也是网上大部分的实现方式,但有时候会有点小问题,代码如下: 只写其实某一段函数 ...
- D - Bridge
n people wish to cross a bridge at night. A group of at most two people may cross at any time, and e ...
- vue2中使用transition
最终效果为 div元素从右向左出现, 然后从左向右消失. transition标签包裹要移动的元素: css 样式: 其中: 1: 为div元素显示时的状态 2: 为div元素移动的过程 (进入 ...
- 清理孤儿文件 clearing up outdated orphans
pacman -Rns $(pacman -Qtdq) It lists all packages installed as dependencies but no longer required b ...
- 网页中顶部banner图自适应css
//test.css .index-banner-top { width: 100%; background: url(../imgs/guanyu.png) no-repeat center cen ...
- mysql limit 性能问题分析
问题重现 // todo 参考文章: MySQL 单表分页 Limit 性能优化 Scalable MySQL: Avoid offset for large tables 证明为什么用limit时, ...