国防科大A
2016-05-16 21:00
323 查看
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
题目要求计算最大的回文重叠数,就是说给定的字符串,你去计算里面的回文数,然后两个如果重叠了,就算出第一个的开始位置和最后一个的位置,统计中间数
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
题目要求计算最大的回文重叠数,就是说给定的字符串,你去计算里面的回文数,然后两个如果重叠了,就算出第一个的开始位置和最后一个的位置,统计中间数
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int str[200]; int huiwen(int n) { int i;
int ans=0; for(i=1;i<=(n+1)/3;i++) { if(jis(n,i)) ans=i; } return ans; } void jis(int n,int m) { int i=n,j=m; for(j=1;j<=m;j++,i--) { if(str[i]==str[i-2*m]&&str[i]==str[i-2*m+2*j-1]) continue; else return 0; } return 1; } int main() { int t,max=0,m; int i;
char a[200]; while(scanf("%d",&t)==1) { while(t--) { cin>>str; m=strlen(str); for(i=2;i<m;i++) { a[i]=huiwen(i); } for(i=2;i<m;i++) { if(max<a[i]) max=a[i]; } cout<<max<<endl; } } return 0; }
相关文章推荐
- Deep Learning学习 之 CNN代码解析(MATLAB)
- 使用Msql统计函数GROUP_CONCAT解决电影票订单表里一条记录多张座位号的问题
- 【Java】接口与抽象类
- 清除mysql表中数据
- 关于数据结构的内存分配问题
- Spark中组件Mllib的学习3之用户相似度计算
- C++:类的成员函数存储方式(是否属于类的对象)
- Zookeeper
- 在windows上通过浏览器远程连接Linux服务器的jupyter
- BeautifulSoup库的常用的函数
- hdu_3932_Groundhog Build Home
- 测试某一天是当年的第几天
- 打包Python程序为exe文件
- 一个有效的与第三方进行网络通信实践
- 【转】调整mac电脑鼠标移动速度
- leetcode
- Codeforces 66B:Petya and Countryside(水题)
- C-贪吃蛇 150809236
- XMPP 协议工作流程详解
- HZAUoj 1015: LCS 【LCS变形】 + 1016: Array C 【贪心】