河南省第四届ACM程序设计大赛 问题 F: SUBSTRING

214人阅读 评论(0)

[提交][状态]

题目描述

You are given a string input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input.

Note well: The substring and its reversal may overlap partially or completely. The entire original string is itself a valid substring . The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.

输入

The first line of input gives a single integer, 1 ≤ N ≤ 10,  the number of test cases. Then follow, for each test case,  a  line  containing between 1 and 50 characters, inclusive. Each character of input will be an uppercase letter ('A'-'Z').

输出

Output for each test case  the longest substring of input such that the reversal of the substring is also a substring of input

样例输入

3
ABCABA
XYZ
XCVCX

样例输出

ABA
X
XCVCX

提示

[提交][状态]

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f

char str[110],s1[110],str2[110];
char Max[110];
int main()
{
int N,i,j,k;
while(scanf("%d",&N)!=EOF)
{
while(N--)
{
scanf("%s",str);
int x=0;
for(i=strlen(str)-1;i>=0;i--)
str2[x++]=str[i];
str2[x]='\0';

int len=(int)strlen(str);
memset(Max,0,sizeof(Max));
for(i=0;i<len;i++)
{
for(j=0;j<=i;j++)
{
int t=0;
for(k=j;k<=i;k++)
{
s1[t++]=str[k];
}
s1[t]='\0';
if( strstr(str2,s1)!=NULL)
{
if((int)strlen(s1)>(int)strlen(Max))
{
strcpy(Max,s1);
}
}
}
}
printf("%s\n",Max);
}
}
return 0;
}

个人资料
等级：
访问量： 9万+
积分： 3690
排名： 1万+
最新评论