题目链接: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. js 保留小数位数

    eg: var num=3.1415926 小数位处理:num.toFixed(n)      n:小数位数

  2. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  3. Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置

    由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Build Lifecycle.Goal).用法(Archetype意义及创建各种项目).maven常 ...

  4. Git中从远程的分支获取最新的版本到本地

    Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge    git fetch origin mastergit l ...

  5. maya 写UI 用到的工具

    import os cmds.window() scrollLayout = cmds.scrollLayout(w=150) cmds.gridLayout( numberOfColumns=30, ...

  6. 第三方:GDataXMLNode:xml解析库--备用

    一.GDataXMLNode说明   GDataXMLNode是Google提供的用于XML数据处理的类集.该类集对libxml2--DOM处理方式进行了封装,能对较小或中等的xml文档进行读写操作且 ...

  7. bzoj 1503: [NOI2004]郁闷的出纳员 Treap

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 6263  Solved: 2190[Submit][Statu ...

  8. app内购提示,您已购买此商品,但未下载

    出现这样的问题,是支付没有finish造成的,一般在支付过程中断网了,下次再购买同一商品的时候就会出现这样的问题, 解决办法,在点击购买的时候判断支付队列中是否有为finish的商品,若有,则进行处理 ...

  9. Struts2 全局拦截器、result 的实现

    定义一个可以继承的包,在这个包里面写入自己常用的拦截器,于是就实现了全局拦截器的实现. 现在,我们定义一个专门用来继承的包: <!--专门提供前台继承的包--> <package n ...

  10. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...