1040. Longest Symmetric String (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1040
题目:
1040. Longest Symmetric String (25)
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)的更多相关文章
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- 1040 Longest Symmetric String (25分)(dp)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT (Advanced Level) 1040. Longest Symmetric String (25)
暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ...
- 【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))
题意: 输入一个包含空格的字符串,输出它的最长回文子串的长度. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/std ...
- PAT 1040 Longest Symmetric String[dp][难]
1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...
- pat1040. Longest Symmetric String (25)
1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
- 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 ...
- 1040 Longest Symmetric String
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
随机推荐
- JavaScript学习总结【2】、JS基础
1.JS 命名规范 命名规范是很有必要的,可增强代码的可读性,一眼就能看懂要表达的意思,规范就是符合规则,使代码有利于后期维护,也能很大程度的提高开发效率.一个正常的网站有很多 JS 代码,如果在编写 ...
- yii2的安装使用
一.Yii2框架 Yii2框架有基本和高级两种版本,主要区别是高级版已经分好了前台.后台,基本版只有前台 二.归档安装方法 归档安装方发很简单,只需要在官网上下载归档文件后,解压即可使用(但是不使用c ...
- WPF 分页控件 WPF 多线程 BackgroundWorker
WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...
- 练习2 G题 - 数值统计
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 统计给 ...
- Apriori算法在购物篮分析中的运用
购物篮分析是一个很经典的数据挖掘案例,运用到了Apriori算法.下面从网上下载的一超市某月份的数据库,利用Apriori算法进行管理分析.例子使用Python+MongoDB 处理过程1 数据建模( ...
- 关于setCharacterEncoding报错
有时候,代码已搬家,就会报这个错,导致这个错误的原因是: HttpServletResponse存在于servlet-api.jar中, 2.3版本的servlet-api.jar中HttpServl ...
- HashMap遍历,推荐使用entrySet()
之前map遍历,偶尔会先去keyset然后再遍历keyset 比如 Map map = new HashMap(); Iterator it = map.keySet().iterator(); wh ...
- 【产品体验】ONE一个
第二篇博客,加油加油~~本人产品新人,学习中,希望大家多多指教! 先来两张ONE的界面图镇楼—— ONE简介: “复杂世界里,一个就够了.”这是一款轻量级的文艺阅读应用,每日更新一张图 ...
- scala 安装
http://www.scala-lang.org/download/install.html http://zh.scala-tour.com/#/hello-wolrd scala指南 To ru ...
- android和struts2实现android文件上传
1.开发准备如下2个工具类 package org.lxh.util; import java.io.BufferedReader; import java.io.InputStreamReader; ...