csuoj-1731-XueXX and P-P String
2016-05-27 09:50
281 查看
Description
XueXX is a clever boy. And he always likes to do something with Palindrome String. What an interesting hobby!
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. A P-P String is a string, which can be divided into three parts with the same length, and part one jointing(拼接) part two generates a palindrome, and part two jointing part three generates a palindrome. That is to say, the string ”abccbaabc” is a P-P String because “abccba” is a palindrome and “cbaabc” is a palindrome.
XueXX’s friend Star can solve the problem whose string’s length is less than 100000. But XueXX cannot. Now give you a short string and can you help XueXX find the longest P-P String?
Input
The first line of input contains the number of test cases T(T<=10). The descriptions of the test cases follow: The first line of each test case contain a string which contains only lowercase letters. Note that the length of the string is less than 200.
Output
For each test case, output a single line containing the result standing the longest length.
Sample Input
3
aaabccbaabc
xxxxxxxxx
abcdefg
Sample Output
9
9
0
一个很简单的字符串题
一个字符串分成三等分,a段+b段要是回文串,b段+c段也要是回文串
然后数据范围并不大,直接分3等分暴力就好了
XueXX is a clever boy. And he always likes to do something with Palindrome String. What an interesting hobby!
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. A P-P String is a string, which can be divided into three parts with the same length, and part one jointing(拼接) part two generates a palindrome, and part two jointing part three generates a palindrome. That is to say, the string ”abccbaabc” is a P-P String because “abccba” is a palindrome and “cbaabc” is a palindrome.
XueXX’s friend Star can solve the problem whose string’s length is less than 100000. But XueXX cannot. Now give you a short string and can you help XueXX find the longest P-P String?
Input
The first line of input contains the number of test cases T(T<=10). The descriptions of the test cases follow: The first line of each test case contain a string which contains only lowercase letters. Note that the length of the string is less than 200.
Output
For each test case, output a single line containing the result standing the longest length.
Sample Input
3
aaabccbaabc
xxxxxxxxx
abcdefg
Sample Output
9
9
0
一个很简单的字符串题
一个字符串分成三等分,a段+b段要是回文串,b段+c段也要是回文串
然后数据范围并不大,直接分3等分暴力就好了
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> using namespace std; char str[500]; bool judge (int l,int r) { //printf("1\n"); for (int i=r,j=r+l-1-l/3;i<j;i++,j--){ if (str[i]!= str[j]) return false; } for (int i=r+l/3,j=r+l-1;i<j;i++,j--){ if (str[i]!=str[j]) return false; } return true; } int main() { int t; scanf("%d",&t); while(t--) { //getchar(); bool flag=false; scanf("%s",str); int l=strlen(str); for(int i=(l/3)*3;i>=0;i-=3) { for(int j=0;j<l;j++) { if (j+i-1>=l) break; if(judge(i,j)) { printf ("%d\n",i); flag=true; break; } } if (flag) break; } } return 0; }
相关文章推荐
- getRequestDispatcher()与sendRedirect()的区别
- 关于UIFont的初始化
- android项目编译报错:error occurred during the build
- ScrollView与UIPageController
- iOS 8UIAlertcontroller如何实现 message左对齐 ,title居中显示
- Minigui-3.0.12 开发记录
- primary key与unique的区别
- iOS中的UIStepper数值加减器用法指南
- iOS开发之高级视图—— UICollectionViewController
- 【Arduino官方教程第一辑】示例程序 5-4 Switch和Case条件语句
- No package identifier when getting value for resource num 0x00000000 【安卓资源找不到】
- NGUI中sprite出现透明边框(黑线)
- NGUI适配问题
- 【CodeForces】675A - Infinite Sequence
- 使用UUID生成验证码
- UITableViewCell添加自定义view,在cell选中高亮时,view消失,取消高亮时,view又出现
- int8 uint8 区别
- Warning: Multiple build commands for output file /xxx
- dispatch_get_main_queue的异步同步问题
- 关于description The requested resource is not available