B - ss
Time limit : 2sec / Memory limit : 256MB
Score : 200 points
Problem Statement
We will call a string that can be obtained by concatenating two equal strings an even string. For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.
You are given an even string S consisting of lowercase English letters. Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S. It is guaranteed that such a non-empty string exists for a given input.
Constraints
2≤|S|≤200
S is an even string consisting of lowercase English letters.
There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.
Input
Input is given from Standard Input in the following format:
S
Output
Print the length of the longest even string that can be obtained.
Sample Input 1
Copy
abaababaab
Sample Output 1
Copy
6
abaababaab itself is even, but we need to delete at least one character.
abaababaa is not even.
abaababa is not even.
abaabab is not even.
abaaba is even. Thus, we should print its length, 6.
Sample Input 2
Copy
xxxx
Sample Output 2
Copy
2
xxx is not even.
xx is even.
Sample Input 3
Copy
abcabcabcabc
Sample Output 3
Copy
6
The longest even string that can be obtained is abcabc, whose length is 6.
Sample Input 4
Copy
akasakaakasakasakaakas
Sample Output 4
Copy
14
The longest even string that can be obtained is akasakaakasaka, whose length is 14.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[210];
while(cin>>s)
{
int len=strlen(s);
len--;//先去掉最后一个
if(len%2!=0)//保证有偶数个
{
len--;
}
int i,j,k;
for(i=len/2;i!=0;len=len-2,i=len/2)//找到字符串的中间位置
{
for(j=0,k=i;k<len;j++,k++)
{
if(s[j]!=s[k])
break;
}
if(k==len)//出现前后相同的字符串
{
break;
}
}
if(i==0)
{
printf("0\n");
}
else
{
printf("%d\n",len);
}
}
return 0;
}

本文介绍了一个算法问题:从给定的偶字符串中删除末尾的一个或多个字符,以找到最长的剩余偶字符串,并给出了实现这一目标的C++代码示例。

被折叠的 条评论
为什么被折叠?



