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的人品实在是太好了,这馅饼 ...
随机推荐
- Java 并发同步器之CountDownLatch、CyclicBarrier
一.简介 1.CountDownLatch是一个同步计数器,构造时传入int参数,该参数就是计数器的初始值,每调用一次countDown()方法,计数器减1,计数器大于0 时,await()方法会阻塞 ...
- iOS后台运行
http://www.cocoachina.com/bbs/read.php?tid=149564 文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后 ...
- POJ 2080
import java.util.*; public class Main { public static void main(String args[]){ Scanner cin=new Scan ...
- Android中的Adapter总结
一.Adapter的介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for t ...
- hdu 4559 涂色游戏(对SG函数的深入理解,推导打SG表)
提议分析: 1 <= N <= 4747 很明显应该不会有规律的,打表发现真没有 按题意应该分成两种情况考虑,然后求其异或(SG函数性质) (1)找出单独的一个(一列中只有一个) (2)找 ...
- 用httpPost对JSON发送和接收的例子
HTTPPost发送JSON: private static final String APPLICATION_JSON = "application/json"; ...
- Android中级之网络数据解析一之xml解析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! --Comic Sans MS Xml解析具有跨平台性,语言无关性,易操作性,因此广受开发者的欢迎. ...
- WPF之快速实现拖拽功能
场景:工具栏上有很多图标(一个图标代表一种控件),可以左键按下图标,然后拖拽到设计区域创建一个个的控件. 实现:1.给工具栏上的图标添加MouseLeftDown和MouseMove事件,前者事件是为 ...
- Ubuntu LAMP搭建
为了数据库课程设计,只好自己搭一个数据库系统,采用LAMP方式. 一.安装 1.安装Apache sudo apt-get install apache2 Apache在安装期间会新建一个目录:/va ...
- Ubuntu安装取色软件
sudo apt-get install Gpick