题意:

  判断是否能将字符串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)的更多相关文章

  1. HDU - 5340 Three Palindromes(manacher算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=5340 题意 判断是否能将字符串S分成三段非空回文串 分析 manacher预处理出前缀和后缀回文的位置, 枚举第 ...

  2. HDU 5340——Three Palindromes——————【manacher处理回文串】

    Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  4. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  5. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  7. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  8. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  9. HDU 1176 免费馅饼 (动态规划)

    HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...

随机推荐

  1. 在wpf窗体上添加用户控件

    1.引用用户控件的命名控件 xmlns:my="clr-namespace:WpfApplicationDemo.Control" 2.把用户控件添加到窗体中 <my:Use ...

  2. Peer certificate cannot be authenticated with known CA certificates.

    I was trying to post to a webservice and was getting the 60 error code: Peer certificate cannot be a ...

  3. POJ 1185 炮兵阵地 (状压DP)

    题目链接 题意 : 中文题不详述. 思路 :状压DP,1表示该位置放炮弹,0表示不放.dp[i][j][k],代表第 i 行的状态为k时第i-1行的状态为 j 时放置的最大炮弹数.只是注意判断的时候不 ...

  4. [转载]Jmeter那点事·ForEach和If控制器

    如果我们要实现一个循环,如果城市是北京,则返回首都:否则,返回城市.   一.新建用户自定义变量 添加-配置元件-用户自定义变量, 定义变量注意命名格式:变量名 加 下划线 加 数字(从1开始计数) ...

  5. ANDROID STUDIO, GRADLE AND NDK INTEGRATION

    Originally posted on:http://ph0b.com/android-studio-gradle-and-ndk-integration/ With the recent chan ...

  6. Include Native *.so Library in APK With Android Studio

    Originally posted on:http://www.kylethielk.com/blog/include-native-so-library-in-apk-with-android-st ...

  7. interviewbit : Max Non Negative SubArrayBookmark Suggest Edit

    Find out the maximum sub-array of non negative numbers from an array.The sub-array should be continu ...

  8. 关于HTTP协议的学习

    HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP 协议的详细内容请参考RFC2616.HTTP协议采用了请求/响应模型.客 ...

  9. iOS开发--UITableView

    -.建立 UITableView  DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  [Data ...

  10. iOS开发--提交github代码

    将的SampleTable例子提交到github 具体步骤如下: a. 首先登陆github b. 创建新的reponsitory name, description c. 打开terminal, c ...