(nyoj308)substring

来源:互联网 发布:齐鲁商品交易软件操作 编辑:程序博客网 时间:2024/05/06 14:32

Substring

时间限制:1000 ms  |  内存限制:65535 KB
难度:1
描述

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


这题很多人跪在题意上,表示我也贡献了一次wa,就是没读懂题意,这道题的意思是找最长子串反过来还是其子串,不是找最长的回文串,举个例子

abcdeba输出ab,因为ab是可以反过来的最长子串;

代码:

#include<iostream>#include<cstdio>#include<string>#include<string.h>#include<algorithm>#include<cmath>#include<vector>#include<cstring>#include<stdlib.h>#include<ctype.h>using namespace std;int main(){    string ch1;    int t,maxlen=1,ans;    cin>>t;    while(t--)    {        cin>>ch1;        string ch2=ch1;        reverse(ch2.begin(),ch2.end());        bool flag=0;        for(int i=ch2.size();i>0;i--)        {             for(int j=0;j<=ch2.size()-i;j++)            {                string v=ch1.substr(j,i);                if(ch2.find(v)!=string::npos)                {                    cout<<v<<endl;                    flag=1;                    break;                }            }            if(flag==1)break;        }    }}


0 0
原创粉丝点击