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. C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值

    接收拦截+发送消息 对于处理所有消息.net 提供了wndproc进行重写 WndProc(ref Message m)protected override void WndProc(ref Mess ...

  2. 根据GridView模板里的列名获取列索引

    以前Insus.NET在写过一篇<在Gridview控件中根据Field Name来取得对应列索引> http://www.cnblogs.com/insus/archive/2010/0 ...

  3. 在OnRowDataBound或OnItemDataBound事件中获取字段值

    不管是在GridView,DataList还是Repeater控件中,其中Repeater控件,没有DataKeyNames或是DataKeyField属性,想获取记录的主键值,只好用Label或是H ...

  4. 读取txt文件的简易算法

    网友在问,从一个文本文件(txt)读取数据,并做简易算法.网友的原问题大约如下, 网友的问题,虽然说是全部是数字,但没有说明是否只有一行.因此Insus.NET在实现算法时,处理文本文件是否多行,是否 ...

  5. linux文件链接

    我的github,欢迎关注,分享知识与技术 链接:一种在共享文件和访问它的用户的若干目录项之间建立联系的一种方法. Linux中包括两种链接:硬链接(HardLink)和软链接(Soft Link), ...

  6. poj1195(二维树状数组)

    题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x< ...

  7. SpringAOP使用

    AspectJ 注解: 1.@Aspect.@Pointcut.Advice @Aspect @Component public class SecurityAspect { @Autowired A ...

  8. 前端开发快速定位bug的一些小技巧

    1,根据报错信息定位: (1) Uncaught TypeError: Cannot read property 'attr' of undefined; 此类型为变量或者对象属性未定义类型. (2) ...

  9. (Python OpenGL)【5】平移 PyOpenGL

    (Python OpenGL) 原文:http://ogldev.atspace.co.uk/www/tutorial06/tutorial06.html  (英文) 下面是我翻译过来的: 背景 在本 ...

  10. CGContext ----生成图片--image

    //获取图片 + (UIImage*) getImageWithColor:(UIColor*)color andHeight:(CGFloat)height { CGRect r= CGRectMa ...