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

最新文章

  1. 2-2. Initializing Objects with Initializer Lists
  2. 【WP 8.1开发】解决调用真实摄像头会死机的问题
  3. Vcenter server 5.5添加用户角色及分配权限
  4. 使 div 元素看上去像一个按钮
  5. Hadoop集群系类文章
  6. eclipse 错误: 找不到或无法加载主类
  7. mui开发app之联网应用传输数据
  8. javascript所有的节点和方法
  9. 3D Game Programming withDX11 学习笔记(一) 数学知识总结
  10. AVStream ddk 翻译
  11. SAP MM 预留单据的历史修改记录?
  12. Lodop打印控件 超文本自动分页
  13. ivew实现table的编辑保存追加删除
  14. 计算元素个数(count和count_if)
  15. CS190.1x Scalable Machine Learning
  16. 1 nodejs
  17. linux -- 查看Ubuntu命令行调用的文件
  18. Android之NFC
  19. Java并发案例02---生产者消费者问题
  20. 火影忍者之~鸣人 (字符串处理,strcmp)

热门文章

  1. struts2(一)之初识struts2
  2. HDU 6121 Build a tree(找规律+模拟)
  3. Matrix Again(最大费用最大流)
  4. 关于php的命名空间
  5. 助你了解react的小demo
  6. Shuffle过程的简单介绍
  7. SSH框架整合--applicationContext.xml文件配置实例
  8. 使用PostgreSQL进行全文检索
  9. Nginx Location 匹配
  10. 数组去重+indexOf()应用