本文主要是介绍(nyoj308)substring,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Substring
- 描述
-
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
这题很多人跪在题意上,表示我也贡献了一次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;}}
}
这篇关于(nyoj308)substring的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!