您的位置:首页 > 产品设计 > UI/UE

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等分暴力就好了

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: