题目链接:http://www.patest.cn/contests/pat-a-practise/1040

题目:

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<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
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 <algorithm>
//先把字符串逆序。然后比較正序和逆序中同样的部分,即为最长回文子串
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;
}

截图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBpZV9DWlg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

——Apie陈小旭

1040. Longest Symmetric String (25)的更多相关文章

  1. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  2. 1040 Longest Symmetric String (25分)(dp)

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  3. PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. PAT (Advanced Level) 1040. Longest Symmetric String (25)

    暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ...

  5. 【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))

    题意: 输入一个包含空格的字符串,输出它的最长回文子串的长度. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/std ...

  6. PAT 1040 Longest Symmetric String[dp][难]

    1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...

  7. pat1040. Longest Symmetric String (25)

    1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

  8. PTA (Advanced Level) 1040 Longest Symmetric String

    1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the lo ...

  9. 1040 Longest Symmetric String

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

随机推荐

  1. vertical-align的深入学习

    W3C官方对vertical-align属性的定义有4个方面:    (1)vertical-align属性用于定义“周围的文字.inline元素以及inline-block元素”相对于该元素基线的垂 ...

  2. 疯狂的表单-html5新增表单元素和属性

    疯狂的表单 2015/11/27 16:44:07 从三方面来介绍html5表单的新特性 表单结构更灵活 要提交数据的控件可以布局在form标签之外,看下面的代码,表单元素可以写到form元素之外,只 ...

  3. PHPExcel导出excel

    如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把$yourStr从utf-8转换成了gb2312: $yourStr = mb_convert_encoding("g ...

  4. 精通 Oracle+Python,第 8 部分:适合 Oracle DBA 使用的 Python

    传统上,当需要为操作系统编写一些脚本时,人们常常会选用 Bash 或 Perl 脚本工具.这些工具易于使用,因而它们几乎变得无处不在,渗透到了包括 Oracle Database 在内的其他软件中,O ...

  5. boost之algorithm/string

    头文件: #include<iostream>#include <boost/algorithm/string.hpp>using namespace std;using na ...

  6. python随机数

    前提:需要导入random模块 >>>import random 1.random.random random.random()用于生成一个0到1的随机符小数: 0 <= n ...

  7. commons-lang阅读(一)

    基于commons-lang-2.4.jar org.apache.commons.lang.time.DateFormatUtils类 package org.apache.commons.lang ...

  8. Mac os 10.9下面配置JAVA_HOME

    刚入手的的MBP,就开始配置java环境,搜了一下网上的都是10.9以前的配置方法.jdk7在10.9的安装目录变化了. 首先到Oracle官网下载最新版本的java,直接默认安装 cd /etc s ...

  9. PHP-mac下的配置及运行

    Here's another option, from the guys from liip, here. This is a PHP package that comes pre-built for ...

  10. Git 忽略已经提交的文件

    如果想在本地忽略某个文件的话执行这个命令: git update-index --assume-unchanged <file> 如果想重新同步这个文件的话执行这个命令. git upda ...