BestCoder Round #49 ($) HDOJ5340 Three Palindromes(暴力)
2015-08-09 10:12
447 查看
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
问一个字符串能不能分成三部分回文
两边枚举字符串 最后判断中间字符串是不是回文就好
AC代码:
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
问一个字符串能不能分成三部分回文
两边枚举字符串 最后判断中间字符串是不是回文就好
AC代码:
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" #include "vector" using namespace std; const int maxn = 20010; char s[maxn]; int len; vector<int> vec[26]; bool ok(int l, int r) { while(l <= r) { if(s[l++] != s[r--]) return false; } return true; } bool judge() { for(int i = 0; i < vec[s[0] - 'a'].size(); ++i) { int u = vec[s[0] - 'a'][i]; if(u + 2 >= len || !ok(0, u)) continue; u++; for(int j = 0; j < vec[s[u] - 'a'].size(); ++j) { int t = vec[s[u] - 'a'][j]; if(t < u) continue; if(t + 1 >= len || s[t + 1] != s[len - 1] || !ok(u, t)) continue; if(ok(t + 1, len - 1)) return true; } } return false; } int main(int argc, char const *argv[]) { int t; scanf("%d", &t); while(t--) { scanf("%s", s); len = strlen(s); for(int i = 0; i < 26; ++i) vec[i].clear(); for(int i = 0; i < len; ++i) vec[s[i] - 'a'].push_back(i); if(judge()) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- seajs使用 - 模块化介绍
- DP问题之最优加法序列
- JQuery函数加载顺序
- Git合并多个Commit
- jQuery轻量级组件Rhui
- Vector介绍
- C++设计模式——适配器模式
- 多个$(document).ready()的执行顺序
- 1026. Table Tennis (30)
- Android四大基本组件介绍与生命周期
- 150809_android_start
- MySQL参数详解及优化
- POJ 1384 Piggy-Bank(DP完全背包)
- HDU 1166 敌兵布阵 线段树
- Git现实(一个)版本控制概述
- 一张图解释谷歌的工作原理
- 分布式缓存的一致性hash算法
- Spring核心之Ioc容器走读笔记
- poj2010 优先队列
- 十进制转换m进制