SUBSTRING

时间限制: 1 Sec  内存限制: 128 MB

提交: 17  解决: 5

[提交][状态][讨论版]

题目描述

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 tiebreaker 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

提示

来源

题目大意:

这道题很容易直接堪称求最长回文子串的题目。但是题目中有一句关键的话。The substring and its reversal may overlap partially or completely.

所以,实际题目的意思是将找到的子串在原串中翻转仍旧出现在原串中。审题很重要。

思路:

字符串最多50个字符,直接爆就好了。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
string s;
cin>>s;
int s_len=s.length();
string ss;
string str;
for(int i=0;i<s_len;i++) {
for(int j=1;j<=s_len-i;j++) {
ss=s.substr(i,j);
reverse(ss.begin(),ss.end());
if(s.find(ss)!=-1) {
if(ss.length()>str.length()) {
reverse(ss.begin(),ss.end());
str=ss;
}
}
}
}
cout<<str<<endl;
}
return 0;
}

第四届河南省ACM SUBSTRING 字符串处理的更多相关文章

  1. 第四届河南省ACM 序号互换 进制转换

    序号互换 时间限制: 1 Sec  内存限制: 128 MB 提交: 41  解决: 19 [提交][状态][讨论版] 题目描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐 ...

  2. 第四届河南省ACM 表达式求值 栈

    表达式求值 时间限制: 1 Sec  内存限制: 128 MB 提交: 14  解决: 7 [提交][状态][讨论版] 题目描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简 ...

  3. 第四届河南省ACM 节能 区间DP

    1001: 节 能 时间限制: 1 Sec  内存限制: 128 MB 提交: 21  解决: 9 [提交][状态][讨论版] 题目描述 Dr.Kong设计的机器人卡多越来越聪明.最近市政公司交给卡多 ...

  4. [河南省ACM省赛-第四届] 序号互换 (nyoj 303)

    相似与27进制的转换 #include<iostream> #include<cstdio> #include<cstring> #include<strin ...

  5. [河南省ACM省赛-第四届] 表达式求值(nyoj 305)

    栈的模拟应用: #include<iostream> #include<cstdio> #include<cstring> #include<string&g ...

  6. js substr和substring字符串截取

    substr(start,length)第一个参数是开始位置(注:start的开始是从0开始,看到好多博客上面是从1开始,在火狐和谷歌执行了一下是从0开始),第二个参数是截取字符串的长度(可以省略,表 ...

  7. Longest Palindromic Substring - 字符串中最长的回文字段

    需求:Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  8. 2017 ACM 字符串的本质

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2017 思路:思考字符串和数字的本质区别是什么. 今天先是试着做了一个完全背包的题目,发现自己还是不会做,弄 ...

  9. 河南省acm第九届省赛--《表达式求值》--栈和后缀表达式的变形--手速题

    表达式求值 时间限制:1000 ms | 内存限制:65535 KB 难度:3   描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, ...

随机推荐

  1. python的小基础

    变量python中的变量为指向常量的地址当常量没有指向时,系统自动回收内存空间如A = 1B = AA = 2print(A,B)#2,1id(A),id(B)id()为python虚拟机的虚拟地址, ...

  2. left join 条件位置问题

    表一:

  3. Ionic3 下拉刷新

    参考: http://ionicframework.com/docs/api/components/refresher/Refresher/

  4. linux下使用crontab实现定时PHP计划任务失败的原因分析

    这篇文章主要介绍了linux下使用crontab实现定时PHP计划任务失败的原因分析,需要的朋友可以参考下   很多人在linux下使用crontab实现PHP执行定时任务却未能成功,不能生成缓存.本 ...

  5. Log4.Net 在Winfrom、MVC、ashx程序里的使用,ashx程序里使用异步

    最近做一个双11活动的,是一套相关的H5页面.本来以为难度不大,但是做下来几天还是遇到些问题.就总结一下吧,还是有收获的. 1.在H5页面中,有一个遮罩层,还是挺有意思的.直接用div+css控制遮罩 ...

  6. Spring学习日志之Bean的装配

    Spring容器负责创建应用程序中的bean并通过依赖注入来协调这些对象之间的关系.但是,作为开发人员,要告诉Spring需要创建哪些bean并且如何将其装配在一起.当描述bean如何装配时,Spri ...

  7. 全排列Permutations

    描述 Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the foll ...

  8. 《天书夜读:从汇编语言到windows内核编程》一 汇编指令与C语言

    1. Debug模式下,VC++6.0下断点运行,按CTRL+F11可查看汇编代码:另外可以用cl /c /FAs YourCppFile.cpp命令行在同目录生成YourCppFile.asm汇编文 ...

  9. bzoj1003

    考虑dp[i]表示前i天的最小总成本. 枚举上一次在第j天之后对路线进行了修改,那么就由dp[j]转移至dp[i],转移的代价是把第[j+1,i]天所有被占用的点全删掉后的最短路(不连通当然就是INF ...

  10. [转载] Hive结构

    转载自http://www.csdn.net/article/2010-11-28/282616 Hive 体系结构 Hive 的结构如图所示 主要分为以下几个部分: 用户接口,包括 CLI,Clie ...