HDU 5340 Three Palindromes (Manacher)
题意:
判断是否能将字符串S分成三段非空回文串。
思路:
先预处理出前缀回文串和后缀回文串的位置,将位置分别装入两个集合中,O(n)。
针对每个前缀回文串的终点位置,挑出不相交的后缀回文串,对中间那段进行暴力匹配即可。只有20个case,不会超时的。
具体的算法参考HIHOCODER HIHO一下 第一周 #1032 : 最长回文子串 (特殊处理)
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <set>
#include <string>
#include <iostream>
#include <deque>
#include <vector>
#define INF 0x7f7f7f7f
#define pii pair<int,int>
#define LL unsigned long long
using namespace std;
const int N=;
int len; //原串长
char str[N*]; //接收原来的串
char s[N*];
int P[N*]; //保存关于长度的信息(回文长度的一半再加1)
vector<int> vect[];
int cal(int q)
{
int id=, mx=, max1=;
P[]=;
P[]=;
for(int i=; s[i]!='\0'; i++) //考虑以i为中心的回文串
{
P[i] =i>mx? : min( P[*id-i],mx-i);
while(s[i+P[i]]==s[i-P[i]]) //在这比对
{
if(i-P[i]==) vect[q].push_back(i+P[i]); //已经匹配
P[i]++;
} if(i+P[i]>mx) //更新id和mx的位置
{
id=i;
mx=i+P[i];
}
if(P[i]->max1) //更新最大值
max1=P[i]-;
}
return max1;
} bool ok()
{
len=strlen(str);
sort(vect[].begin(), vect[].end());
sort(vect[].begin(), vect[].end()); for(int i=; i<vect[].size(); i++)//这里虽然穷举了所有可能,但通常都很少
{
for(int j=; j<vect[].size(); j++)
{
int l=vect[][i]+;
int r=len-vect[][j]-;
if(l>r) break; while( l<r && str[l]==str[r] )
l++,r--;
if(l==r && str[l]==str[r])
return true; }
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
scanf("%s",str);
len=strlen(str);
vect[].clear();
vect[].clear();
if(len==)///特别处理
{
puts("YES");
continue;
} s[]='$';
s[]='#';
int i=, j=;
for(; i<len; i++)
{
s[j++]=str[i];
s[j++]='#';
}
s[j]='\0';
strcpy(str,s);//先copy出一份拷贝
memset(P, , sizeof(P));
cal(); reverse(s,s+*len+);//反置过来,求后缀回文
s[]='$';
s[*len+]='\0';
memset(P, , sizeof(P));
cal(); if(ok())puts("Yes");
else puts("No");
}
return ;
}
AC代码
HDU 5340 Three Palindromes (Manacher)的更多相关文章
- HDU - 5340 Three Palindromes(manacher算法)
http://acm.hdu.edu.cn/showproblem.php?pid=5340 题意 判断是否能将字符串S分成三段非空回文串 分析 manacher预处理出前缀和后缀回文的位置, 枚举第 ...
- HDU 5340——Three Palindromes——————【manacher处理回文串】
Three Palindromes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- HDU 1176 免费馅饼 (动态规划)
HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...
随机推荐
- DELPHI 获取本月 的第一天 和 最后一天
USER :DateUtils 使用 StartOfTheMonth 和 EndOfTheMonth 函数获取即可: procedure TForm1.btn1Click(Sender: TObj ...
- javascript的Function 和其 Arguments
http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...
- Android中将布局文件/View添加至窗口过程分析 ---- 从setContentView()谈起
本文主要内容是讲解一个视图View或者一个ViewGroup对象是如何添加至应用程序窗口中的.下文中提到的窗口可泛指我们能看到的界面,包括一个Activity呈现的界面(我们可以将之理解为应用程序窗口 ...
- ExtJs之addManagedListener
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- (转)Android之ListView原理学习与优化总结
转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...
- Node 出现 uncaughtException 之后的优雅退出方案
Node 的异步特性是它最大的魅力,但是在带来便利的同时也带来了不少麻烦和坑,错误捕获就是一个.由于 Node 的异步特性,导致我们无法使用 try/catch 来捕获回调函数中的异常,例如: try ...
- 【HDU 5030】Rabbit's String (二分+后缀数组)
Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...
- Android核心分析之二十八Android GDI之Surface&Canvas
Surface&Canvas Canvas为在画布的意思.Android上层的作图几乎都通过Canvas实例来完成,其实Canvas更多是一种接口的包装.drawPaints ,drawPoi ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-06po层
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- C#之你懂得的反射
前言:反射在C#中虽然不常用(如果不需要动态加载xx.dll),但是有时候却是设计某个程序或者完成某类功能比较好用的技术.比如:一个支持动态扩展的程序,这样就需要动态加载dll,动态创建加载dll的程 ...