hdu 5340 Three Palindromes
2015-08-07 19:26
204 查看
题解:
1.马拉车算法预处理
2.枚举两边回文串的可能情况
3.因为枚举中间的可能会在面对这样的数据时比较麻烦——-abababab,相对来说,枚举两边也是只快了一点(我枚举中间T了,枚举两边500ms+)
1.马拉车算法预处理
2.枚举两边回文串的可能情况
3.因为枚举中间的可能会在面对这样的数据时比较麻烦——-abababab,相对来说,枚举两边也是只快了一点(我枚举中间T了,枚举两边500ms+)
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <map> using namespace std; #define MAXN 20005 #define INF 0x3f3f3f3f int _,d[MAXN << 1],len,pre[MAXN << 1],suf[MAXN << 1]; char cs[MAXN],s[MAXN << 1]; void Manacher() { len = strlen(cs); s[0] = '$'; for(int i = 0;i <= len;i++) { s[i << 1 | 1] = '#'; s[(i << 1) + 2] = cs[i]; } int idx = 0,maxn = 0; for(int i = 1;i <= (len << 1);i++) { if(maxn > i) d[i] = min(d[idx * 2 - i],maxn - i); else d[i] = 1; for(;s[i + d[i]] == s[i - d[i]];d[i]++); if(i + d[i] > maxn) { maxn = d[i] + i; idx = i; } } } bool solve() { int cnt1 = 0,cnt2 = 0; for(int i = 2;i <= (len << 1);i++) { if(d[i] == i) pre[cnt1++] = i * 2 - 1; if(d[i] + i == (len << 1) + 2) suf[cnt2++] = i - d[i] + 1; } //for(int i = 0;i < cnt2;i++)cout << suf[i] << " pre "; //cout << endl; for(int i = 0;i < cnt1;i++) for(int j = 0;j < cnt2;j++)if(pre[i] + 1 < suf[j]) { //cout << pre[i] << " p s " << suf[i] << endl; int gap = suf[j] - pre[i]; if(gap == 2 && s[pre[i] + 1] != '#')return true; if(d[gap / 2 + pre[i]] * 2 >= gap)return true; } return false; } int main() { scanf("%d",&_); while(_--) { scanf("%s",cs); Manacher(); //for(int i = 1;i <= 6;i++)cout << d[i] << " "; if(solve())puts("Yes"); else puts("No"); } }
相关文章推荐
- C++(13)STL实践与分析之再谈String类型
- 简单工程模式--计算机
- 【CF245H】【Queries for Number of Palindromes】
- POJ-2196 Specialized Four-Digit Numbers-10,12,16进制的各个位数相加彼此相等的数
- JavaScript——实现时间的动态显示
- B. ZgukistringZ-简单字符串处理-Codeforces Round #307 (Div. 2)
- MySQL使用说明
- 在Linux中创建静态库.a和动态库.so
- 熟人Dubbo 系列1-Dubbo什么
- 在windows 、linux下读取目录下所有文件名
- hdu 5363 Key Set(水)
- javascriptcore.framework学习.
- hdu 5361 In Touch(最短路+并查集)
- 杭电acm 2139 Calculate the formula
- Java IO流--IO包中的其他类
- 【bzoj2141】 排队 树状数组+主席树
- Python简单爬虫
- HDU 5355 Cake(数学 DFS)
- POJ-2136 Vertical Histogram-用*号统计字母个数
- C++(12)STL实践与分析之顺序容器