博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1040. Longest Symmetric String (25)
阅读量:7126 次
发布时间:2019-06-28

本文共 1496 字,大约阅读时间需要 4 分钟。

题目链接:

题目:

1040. Longest Symmetric String (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?

", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11.

Input Specification:

Each input file contains one test case which gives a non-empty string of length no more than 1000.

Output Specification:

For each test case, simply print the maximum length in a line.

Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11

分析:

找到一个字符串的最长回文子串,那个时候还不知道kmp,manacher,用比較搓的方法做出来的。

AC代码:

#include
#include
#include
#include
using namespace std;int main(){ //freopen("F://Temp/input.txt", "r", stdin); string str; string str_r; getline(cin,str); str_r = str; reverse(str_r.begin(),str_r.end()); //included in the
//先把字符串逆序。然后比較正序和逆序中同样的部分,即为最长回文子串 int same = 0, sum = 0; for (int i = 0; i < str.size(); i++){ sum = 0; for (int j = 0, rj = i; j < str.size() - i; j++,rj ++){ if (str_r[rj] == str[j]){ sum++; if (sum > same)same = sum; } else sum = 0; } sum = 0; for (int j = 0, rj = i; j < str.size() - i; j++, rj++){ if (str_r[j] == str[rj]){ sum++; if (sum > same)same = sum; } else sum = 0; } } cout << same << endl; return 0;}


截图:

本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5312834.html,如需转载请自行联系原作者

你可能感兴趣的文章
iptables详解
查看>>
Redisson官方文档 - 12. 独立节点模式
查看>>
AD域笔记
查看>>
HTTP协议详解
查看>>
apache实现多端囗多域名配置
查看>>
Linux命令(15):type命令
查看>>
第一单元作业
查看>>
Azure云端部署Exchange 2016双数据中心—Part6(DAG切换测试)
查看>>
通过ansible部署高可用LNAMMKP架构
查看>>
IBM Aix系统添加硬盘步骤
查看>>
“esxcli software vib” commands to patch an ESXi 5.x/6.x host (2008939)
查看>>
heartbeat管理与虚拟IP介绍
查看>>
Syslog-ng+Rsyslog收集日志:RELP可靠传输,替代UDP、TCP(五)
查看>>
课程第八天内容《基础交换八》补充案例
查看>>
ionic 之 基本布局
查看>>
nginx开启目录浏览
查看>>
32位Linux设置超大Oracle SGA的分析
查看>>
const 的用法总结
查看>>
2017企业网盘年终盘点|机遇与挑战并存,寡头显现
查看>>
将linux用在开发环境中
查看>>