NYOJ 308 Substring 字符串处理问题

      这道题我也是花了好长时间才写出来,本来想出来方法了,带式写着太麻烦,所以就一直没写,一直想用string类写的,但是由于对string类中的函数不清楚,所以基本上把每个函数都试了一遍,,,,好在写出来了。这道题题意容易搞错,去年省赛时就有许多队伍理解错题意了,此题不是让求最长回文子串的,而是让求一个最长的子串,该子串的逆串也在原串中。举个例子来说,abcdba,若是求最长回文子串,应为a,对此题来说,应为ab,因为ab的逆串ba也在原串中。题目:

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                   
ABCABA
XYZ
XCVCX
样例输出
ABA
X
XCVCX
ac代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
int main(){
	//freopen("1.txt","r",stdin);
	int numcase;
	scanf("%d",&numcase);
	string s1,s2,s3;
	while(numcase--){
	  cin>>s1;
	  s2=s1;
	  int max=0;
	  reverse(s2.begin(),s2.end());
	  int len=s1.size();
	  for(int i=0;i<len;++i){
		  for(int j=1;j<=len-i;++j){
			  string::size_type pos=s2.find(s1.substr(i,j));
			  if(pos!=string::npos){
				  if(max<j){
				    max=j;
					s3=s1.substr(i,j);
				  }
			  }
		  }
	  }
	  cout<<s3<<endl;
	}
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值