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

提交代码

方法一:插入无效字符,遍历一次即可。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
int dp[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
//scanf("%s",s);
string s;
getline(cin,s); //cout<<s<<endl; int i,j=,k,count=;
dp[j++]=-;
for(i=;i<s.length();i++){
dp[j++]=s[i];//hash
dp[j++]=-;
} //cout<<j<<endl; for(i=;i<j;i++){//i从1开始!!
int f=i-,b=i+;
while(f>=&&b<j&&dp[f]==dp[b]){
f--;
b++;
}
if(count<b-f-){
count=b-f-;
//cout<<count<<endl;
}
}
printf("%d\n",count/);//这里可以分为中心为-1和正常数字 2种情况讨论
return ;
}

方法二:分奇偶别讨论:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
//scanf("%s",s);
string s;
getline(cin,s); //cout<<s<<endl; int i,j,k,count=;
for(i=;i<s.length();i++){
int f=i,b=i;
while(f>=&&b<s.length()&&s[f]==s[b]){
f--;
b++;
}
if(count<b-f-){
count=b-f-;
}
f=i;
b=i+;
while(f>=&&b<s.length()&&s[f]==s[b]){
f--;
b++;
}
if(count<b-f-){
count=b-f-;
}
}
printf("%d\n",count);
return ;
}

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

  1. PAT1040 Longest Symmetric String (25分) 中心扩展法+动态规划

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

  2. 1040. Longest Symmetric String (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...

  3. PAT1040:Longest Symmetric String

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Hyper-V 中遇到错误 'vm' could not initialize

    不知道前面做了什么操作,但当我启动我的虚拟机时遇到了'vm' could not initialize的错误,如下图: 在几番尝试未果后,找到了如下解决方法: 以管理员模式启动cmd,执行命令 net ...

  2. WinForm 生命周期, WinForm 事件执行顺序

    1.窗体启动: Control.HandleCreatedControl.BindingContextChangedForm.LoadControl.VisibleChangedForm.Activa ...

  3. Vistual Studio的导出模板功能

    应用场景,每个项目有自己固定的目录结构和引用文件, 无需每次创建一个项目,就手工一一将那些目录再new一遍.如图所示 菜单  文件=>导出模板 之后的操作基本上一路"下一步" ...

  4. C#中如何防止Excel做科学计算法转换

    C#中如何防止Excel做科学计算法转换  string style = @"<style>.text{mso-number-format:\@;}</style>& ...

  5. Lucene 全文检索 Lucene的使用

    Lucene  全文检索  Lucene的使用 一.简介: 参考百度百科: http://baike.baidu.com/link?url=eBcEVuUL3TbUivRvtgRnMr1s44nTE7 ...

  6. MySQL运维及开发规范

    一.基础规范 (1) 使用INNODB存储引擎 (2) 表字符集使用UTF8 (3) 所有表都需要添加注释 (4) 单表数据量建议控制在5000W以内 (5) 不在数据库中存储图.文件等大数据 (6) ...

  7. [Django笔记] admin 深入学习

    admin django 内置的管理后台,大部分时候可以通过对admin进行配置来提高开发效率. 数据列表展示 默认情况下显示一个models-objects的列表,如果model定义了 __str_ ...

  8. Linux 下的 etc

    /etc etc不是什么缩写,是and so on的意思 来源于 法语的 et cetera 翻译成中文就是 等等 的意思. 至于为什么在/etc下面存放配置文件, 按照原始的UNIX的说法(linu ...

  9. C++学习笔记-关键词

    1.friend友元 采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口.但是,有时需要定义一些函数,这些函数不是类的一部分( ...

  10. 在SQLSERVER中创建聚集索引

    CREATE CLUSTERED INDEX CLUSTER_id ON TABLE_name(ID)------批量