HDU - 5340 Three Palindromes(manacher算法)
http://acm.hdu.edu.cn/showproblem.php?pid=5340
题意
判断是否能将字符串S分成三段非空回文串
分析
manacher预处理出前缀和后缀回文的位置, 枚举第一个回文串和第三个回文串,这样得到第二个回文串的区间,找中点,因为manacher处理后所有的回文串长度都是奇数,然后根据中点的回文半径判断中间部分是否回文即可, 复杂度o(n2)。至于n2复杂度为什么能水过去。。不是很懂
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int N = 1e6+;
//const int MAXN = 210;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T;
void testcase(){
printf("Case #%d: ",++T);
}
const int MAXN = 4e4+;
const int MAXM = ;
char ma[MAXN],s[MAXN];
int mp[MAXN];
int pre[MAXN],suf[MAXN];
void Manacher(int len){
int l=;
ma[l++]='$';
ma[l++]='#';
for(int i=;i<len;i++) ma[l++]=s[i],ma[l++]='#';
ma[l]=;
int mx=,id=;
for(int i=;i<l;i++){
mp[i]=mx>i?min(mp[*id-i],mx-i):;
while(ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++;
if(i+mp[i]>mx){
mx=i+mp[i];
id=i;
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t;
scd(t);
while(t--){
scanf("%s",s);
int len = strlen(s);
Manacher(len);
int l=,r=;
len=*len+;
for(int i=;i<=len;i++){
if(mp[i]==i&&i!=) pre[l++]=mp[i];
//mp[i]==i保证mp[i]可以作为第一个回文串的半径,加入pre数组,i!=1保证第一个回文串不为空
if(mp[i]+i-==len&&i!=len) suf[r++]=mp[i];
}
int i,j;
for(i=l-;i>=;i--){
for(j=;j<r;j++){
int t1=*pre[i];
int t2=len+-*suf[j];
int tmp = (t1+t2)>>;
if(t1>t2) continue;
if(mp[tmp]==) continue;
if(mp[tmp]*->=t2-t1+) break;
}
if(j<r) break;
}
if(i>=) puts("Yes");
else puts("No");
}
return ;
}
HDU - 5340 Three Palindromes(manacher算法)的更多相关文章
- hdu 5340 Three Palindromes
前几晚 BC 的第二题,官方给出的题解是: 然后我结合昨天刚看的 Manacher 算法试着写了下,发现 pre.suf 数组挺难构造的,调试了好久,然后就对中间进行枚举了,复杂度应该是 O(n2) ...
- hdu 3613"Best Reward"(Manacher算法)
传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...
- HDU 5340——Three Palindromes——————【manacher处理回文串】
Three Palindromes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5340 Three Palindromes (Manacher)
题意: 判断是否能将字符串S分成三段非空回文串. 思路: 先预处理出前缀回文串和后缀回文串的位置,将位置分别装入两个集合中,O(n). 针对每个前缀回文串的终点位置,挑出不相交的后缀回文串,对中间那段 ...
- Manacher 算法(hdu 3068 && hdu 3294)
今天打算补前晚 BC 的第二题,发现要用到能在 O(n) 时间求最大回文子串长度的 Manacher 算法,第一次听,于是便去百度了下,看了大半天,总算能看懂了其思想,至于他给出的代码模板我没能完全看 ...
- hdu 3068 最长回文 manacher算法(视频)
感悟: 首先我要Orz一下qsc,我在网上很难找到关于acm的教学视频,但偶然发现了这个,感觉做的很好,链接:戳戳戳 感觉这种花费自己时间去教别人的人真的很伟大. manacher算法把所有的回文都变 ...
- hdu5340—Three Palindromes—(Manacher算法)——回文子串
Three Palindromes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
- HDU 3068:最长回文(Manacher算法)
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的 ...
随机推荐
- Notepad++找回Plugin Manager{在v7.50后(包括7.50)不带有插件管理器(Plugin Manager)}
https://github.com/notepad-plus-plus/notepad-plus-plus/issues/2459 64 bit Plugin Manager is now avai ...
- Oracle PLSQL 客户端 连接Oracle12.2 出现权限问题的解决办法以及绿色版Oracle客户端的使用.
1. 同事反馈登录最新的oracle12.2 的数据库时登录不上报错: 2. 记得当时查过资料, Oracle 在12.2 增加了客户端连接数据库的加密级别 比较早的oracle客户端比如11.2.0 ...
- [转]ubuntu ssh登陆显示系统信息
Welcome to Ubuntu LTS (GNU/Linux --generic i686) * Documentation: https://help.ubuntu.com/ System in ...
- codeforces710B
Optimal Point on a Line CodeForces - 710B You are given n points on a line with their coordinates xi ...
- windows编程按小时生成日志文件
这是一个简单的日志记录方法,为了避免单个日志文件过大,所以每个小时生成一个新的日志文件 注意:g_pLogPath 可以带路径,但是必须手动创建好路径,保证目录存在.而且要详细到log文件名,不能带后 ...
- docker--compose--sonarqube
Create this docker-compose.yml file: version: "2" services: sonarqube: image: sonarqube po ...
- Link-Cut Tree(LCT)&TopTree讲解
前言: Link-Cut Tree简称LCT是解决动态树问题的一种数据结构,可以说是我见过功能最强大的一种树上数据结构了.在此与大家分享一下LCT的学习笔记.提示:前置知识点需要树链剖分和splay. ...
- 数字对——RMQ+二分答案
题目描述 小H是个善于思考的学生,现在她又在思考一个有关序列的问题. 她的面前浮现出一个长度为n的序列{ai},她想找出一段区间[L, R](1 <= L <= R <= n). 这 ...
- wamp下var_dump()相关问题
PHP 使用var_dump($arr)时 没有格式化输出. 原因是没有启用‘XDebug’扩展 [xdebug]zend_extension ="d:/wamp/bin/php/php7. ...
- MT【17】利用柯西不等式求三角的最大值
评:此题也可以设$1+cos\theta=t$,平方后变成$t$的单变量利用均值去做. 柯西平衡系数法其实就是待定系数法,利用等号取到的条件.