HDU 5340 Three Palindromes(Manacher乱搞)
2015-08-03 08:13
323 查看
Problem Description
Can we divided a given string S into three nonempty palindromes?
Input
First line contains a single integer T≤20 which
denotes the number of test cases.
For each test case , there is an single line contains a string S which only consist of lowercase English letters.1≤|s|≤20000
Output
For each case, output the "Yes" or "No" in a single line.
Sample Input
Sample Output
分析:Manacher之后乱搞。
Can we divided a given string S into three nonempty palindromes?
Input
First line contains a single integer T≤20 which
denotes the number of test cases.
For each test case , there is an single line contains a string S which only consist of lowercase English letters.1≤|s|≤20000
Output
For each case, output the "Yes" or "No" in a single line.
Sample Input
2 abc abaadada
Sample Output
Yes No
分析:Manacher之后乱搞。
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; const int maxn=1e5+100; char str[maxn],ma[maxn]; int mp[maxn],len,t,p[maxn]; vector<int>pre; vector<int>suf; void Manacher() { int l=0; ma[l++]='$';ma[l++]='#'; for(int i=0;i<len;i++) { ma[l++]=str[i]; ma[l++]='#'; } ma[l]='\0'; len=l-1; int tot=0; int mx=0,id=0,ans=0; for(int i=0;i<l;i++) { mp[i]=mx>i?min(mp[2*id-i],mx-i):1; while(ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++; if(i+mp[i]>mx) { mx=i+mp[i]; id=i; } } } int main() { scanf("%d",&t); while(t--) { scanf("%s",str); pre.clear();suf.clear(); len=strlen(str); if(len<3) { puts("No"); continue; } Manacher(); for(int i=1;i<=len;i++) { if(mp[i]==i) pre.push_back(i); if(i+mp[i]-1==len) suf.push_back(i); } int flag=0; int presz=pre.size(); int sufsz=suf.size(); for(int i=0;i<presz;i++) { int a=pre[i]; for(int j=sufsz-1;j>=0;j--) { int b=suf[j]; if(a>=b) break; int L=a+mp[a]; int R=b-mp[b]; if(L<=2||R>=len-1) continue; if(L==R&&ma[L]=='#') continue; if(L>R) break; int sz=R-L+1; int mid=(L+R)>>1; if(mp[mid]*2-1>=sz) flag=1; } if(flag) break; } puts(flag?"Yes":"No"); } return 0; } /* 2 aaddaaa */
相关文章推荐
- Linux内核驱动模块编译
- ios--c DAY_4
- [周赛] HDU - 4970 Killing Monsters
- NOI2013 Day2
- Skewness
- 用户和用户组的学习
- OC连载四-----Foundation框架——字典、日期、异常
- ios--c DAY_3
- myeclipse无法新建jsp页面
- C:进制
- (笔记)鼠标移动到一个 元素上所触发的事件
- 黑马程序员——自学java基础课程第二…
- 【bzoj1079】【SCOI2008】【着色方案】
- hibernate一对多等情况关联查询效率低的解决方法
- 素数筛法
- 【Unity游戏开发之五】游戏目录结构之最佳实践和优化
- 素数筛法
- Linux 桌面领域的八大最新潮流
- Linux 桌面领域的八大最新潮流
- POJ 2251 Dungeon Master