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

来源:互联网 发布:锦衣卫同知是什么官职 编辑:程序博客网 时间:2024/04/27 13:48

问题 F: SUBSTRING

时间限制: 1 Sec  内存限制: 128 MB
提交: 30  解决: 22
[提交][状态]

题目描述

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                   ABCABAXYZXCVCX

样例输出

ABAXXCVCX

提示

[提交][状态]

比较水的一道题:意思就是求 所给字符串,正反串所包含的最长子串,数据较小,直接暴力一下。

#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;
}

0 0