Gym10198-Mediocre String Problem-2018南京ICPC现场赛
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
Catalog
@
Problem:传送门
原题目描述在最下面。
Solution:
- 二分+\(hash\)+\(manacher\)
- 题意要我们在s串中找一个子串,和在t串中找一个前缀串,组合成回文串,但是串1长度要大于串2的长度。
- 其实就是要找三个串,在s中找连续的串a和串b,在t中找一个前缀串c
- 满足b是回文串且长度至少为1,a串和c串对称
- 到这一步思路就十分明确了,可以用\(manacher\)预处理出以\(i\)点为左端点的串是回文串的个数,差分一下就可以了。
- 然后就很简单了,先把t串反转。
- 枚举\(i\),\(i\)表示a串的末尾,则\(i+1\)就是b串的起点,然后求s串[0,i]的子串和反转后t串的最长公共后缀,这一步就直接二分+\(hash\)就可以了。累计贡献\(len_{最长公共后缀}\times pre[i+1]\)
AC_Code:
#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define fi first
#define se second
#define lowbit(x) (x&(-(x)))
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
const int N = 1e6+7;
const int MXN = 1e6+7;
const long long MOD = 2078526727;
const long long BASE = 1572872831;
LL pw[MXN], hs1[MXN], hs2[MXN];
int n, m;
char ar[N<<1], br[N<<1], t[N];
int p[N<<1], pre[N];
void manacher(){
int Len = strlen(ar), id = 0, ans_id = 0, right = 0;
memset(p, 0, sizeof(p));
memset(br, 0, sizeof(br));
for(int i = Len; i >= 0; --i){
ar[2 * i + 1] = '#';
ar[2 * i + 2] = ar[i];
}
ar[0] = '*';p[0] = p[1] = 1;
for(int i = 2; i < 2 * Len + 1; ++i){
if(i < right)p[i] = min(p[2 * id - i], right - i);
else p[i] = 1;
while(i - p[i] >= 0 && ar[i + p[i]] == ar[i - p[i]])p[i]++;
if(p[ans_id] < p[i])ans_id = i;
if(i + p[i] > right){
id =i;
right = i + p[i];
}
}
}
LL get(LL *a, int l, int r) {
return ((a[r]-a[l-1]*pw[r-l+1])%MOD+MOD)%MOD;
}
bool ok(int len, int P, int lent) {
if(get(hs1, P-len+1, P) == get(hs2, lent-len+1, lent)) return true;
return false;
}
int main() {
pw[0] = 1;
for(int i = 1; i < 1000003; ++i) pw[i] = pw[i-1] * BASE % MOD;
scanf("%s%s", ar, t);
int len = strlen(ar);
hs1[0] = hs2[0] = 1;
for(int i = 0; i < len; ++i) {
hs1[i+1] = (hs1[i]*BASE + ar[i]) % MOD;
}
manacher();
for(int i = 2; i < 2 * len + 1; ++ i) {
pre[(i-p[i]+2)/2-1] ++;
pre[i/2] --;
//printf("%d %d %d %d\n", i, p[i], (i-p[i]+2)/2-1, i/2);
}
for(int i = 1; i < len; ++i) {
pre[i] += pre[i-1];
}
/*for(int i = 0; i < len; ++i) {
printf("%d ", pre[i]);
}
printf("\n");*/
int lent = strlen(t);
reverse(t, t + lent);
for(int i = 0; i < lent; ++i) {
hs2[i+1] = (hs2[i]*BASE + t[i]) % MOD;
}
int k = 0;
for(int i = 2; i < 2*len-1; ++i) {
if(i%2 == 0) ar[k++] = ar[i];
}
LL res = 0;
for(int i = 0; i < len - 1; ++i) {
//二分LCP,[?,i],[1,?]
if(ar[i] != t[lent-1]) continue;
int l = 1, r = min(lent, i+1), mid, ans = 0;
while(r >= l) {
mid = (l+r)>>1;
if(ok(mid, i+1, lent)) ans = mid, l = mid+1;
else r = mid-1;
}
res += (LL)ans * pre[i+1];
//printf("%d %d\n", i, ans);
}
printf("%lld\n", res);
return 0;
}
Problem Description:
Gym10198-Mediocre String Problem-2018南京ICPC现场赛的更多相关文章
- 2018 南京icpc现场赛总结
Day 0 提前5个小时从学校出发,在登机口坐下时,飞机还有1个多小时起飞. 航班准时起飞,到了南京以后直接坐地铁到学校附近(南京地铁票也太精致了吧). 因为天已经黑了,就只在学校附近转了一圈就回酒店 ...
- 2018南京icpc现场赛心得
第一次参加icpc的比赛,也是第一块奖牌,虽然只是铜,但其实打的已经很好了,稍微差一点就可以摸银了. 之前参加省赛,成为那次比赛我校唯一一个没拿奖的队伍,其实还是一直都有一些心结的,而这段时间和新的队 ...
- 2018 焦作icpc现场赛总结
Day 0 没有直达焦作的飞机,所以选择了先到新郑机场,再转乘城际列车.城际列车猜是专门给学生开通的吧,每天只有来和回一共两趟(所以机票选择的余地也不多).买的时候只有无座票了,本来以为会一直站着,但 ...
- ACM-ICPC 2018 南京赛区现场赛 E. Eva and Euro coins (思维)
题目链接:https://codeforc.es/gym/101981/attachments 题意:给出两个只包含01的字符串,每次可以选择连续k个相同的数字进行翻转,问能否通过若干次操作把两个字符 ...
- ACM-ICPC 2018 南京赛区现场赛 K. Kangaroo Puzzle (思维+构造)
题目链接:https://codeforc.es/gym/101981/attachments 题意:在 n * m 的平面上有若干个袋鼠和墙(1为袋鼠,0为墙),每次可以把所有袋鼠整体往一个方向移动 ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- ACM-ICPC2018南京赛区 Mediocre String Problem
Mediocre String Problem 题解: 很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash). 然后剩下的就是要处理以 ...
- 2013ACM/ICPC亚洲区南京站现场赛---Poor Warehouse Keeper(贪心)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4803 Problem Description Jenny is a warehouse keeper. ...
- 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的长 ...
随机推荐
- 【HANA系列】SAP HANA的特点总结
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA的特点总结 ...
- DS静态查找- 顺序-二分-索引
静态查找 静态表是只执行查找操作,而不执行插入.删除等操作的表. 现在常说的有五大查找方法:顺序查找.分块查找.索引查找.树查找.哈希查找. 后两种之前写过了二叉查找树和哈希表,现在回顾前面三种,它们 ...
- JavaScript Is or isNot
读书笔记,简化代码--不对外公布,只是做笔记使用. var superman = { name: "Superman", strength: "Super", ...
- LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号
https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...
- Codeforces 1105D (BFS)
题面 传送门 分析 考虑BFS while(棋盘没有满){ for 玩家 p{ 对p进行BFS,走s[p]步 } } 对于每个玩家p BFS的时候如果到了格子(x,y),就把\(vis[x][y]\) ...
- 常见前端面试题CSS部分
1.盒模型 IE 盒子模型:IE的width部分包含了 border 和 pading; 标准 W3C 盒子模型: width/height+border+padding+margin; 2.清除浮动 ...
- XMPP即时通讯协议使用(五)——搭建简单的Openfire插件
前言 在开发Openfire插件前需要构建完成服务器源码编辑环境,具体操作步骤请参照Openfire服务器源码编译的了解. 开发简单的Openfire插件 1.已构建完成的Openfire源码结构如下 ...
- hadoop_hdfs_上传文件报错
错误提示: INFO hdfs.DFSClient: Exception in createBlockOutputStream java.io.IOException: Bad connect ack ...
- k8s阅读笔记2-k8s架构
前言 阅读地址 https://rootsongjc.gitbooks.io/kubernetes-handbook/content/concepts/ 架构 架构图说明: master 指服务端 1 ...
- Git 协作:Fetch Pull Push Branch Remote Rebase Cherry-pick相关
前言 学习git的时候,我们首先学习的是最常用的,自己独立开发Software时用的命令: git init //初始化git仓库 git add <file_name> //将文件添加到 ...