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的人品实在是太好了,这馅饼 ...
随机推荐
- UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)
题目链接:http://acm.upc.edu.cn/problem.php?id=2224 题意:给出n个数pi,和m个查询,每个查询给出l,r,a,b,让你求在区间l~r之间的pi的个数(A< ...
- iOS-CALayer实现简单进度条
/** * 用CALayer定制下载进度条控件 * 1.单独创建出CALayer * 2.直接修改CALayer的frame值,执行隐式动画,实现进度条效果 * 3.用定时器(NSTimer) ...
- POJ 1573
#include<iostream> #include<stdio.h> #define MAXN 15 using namespace std; char _m[MAXN][ ...
- POJ 1504
#include<iostream> using namespace std; int main() { int num; cin>>num; while(num--){ in ...
- C# TryXXXX模式
public static int? TrayParse(string text) { int ret; if (int.TryParse(text,out ret)) { return ret; } ...
- sql server 数据库 数据DateTime 转mysql
首先将sql server DateTime 转换为varchar(50) 然后更新转换过的 DateTime字段, UPDATE 表名 SET LastUpdateTime=CONVERT(VAR ...
- ExtJs之文本框及数字输入
结合HTML来理解, 比较容易. <!DOCTYPE html> <html> <head> <title>ExtJs</title> &l ...
- 《JavaScript基础教程(第8版)》PDF
简介:JavaScript基础教程(第8版)循序渐进地讲述了JavaScript及相关的CSS.DOM.Ajax.jQuery等技术.书中从JavaScript语言基础开始,分别讨论了图像.框架.浏览 ...
- 2016CVTE编程题:兔子藏洞
兔子藏洞 题目描述 一只兔子藏身于20个圆形排列的洞中(洞从1开始编号),一只狼从x号洞开始找,下次隔一个洞找(及在x+2号洞找),在下次个两个洞找(及在x+5号洞找),以此类推...它找了n次仍然没 ...
- unity Transform Find 的用法!!!
用法: Transform Find(String name) 1.查找名为name的(transform.gameObject)直接子物体并返回该子物体的Transform属性.不能是孙子物体或更低 ...