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. 校对双层PDF中的隐藏文本

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2012.06.11 目录一.背景二.能够校对的PDF需要满足的条件三.校对工具的选择四.校对过程五.延伸讨论 事先声明:本文 ...

  2. linux 进程间通信机制(IPC机制)一消息队列

    消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法.每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构.我们可以通过发送消息来避免命名管道的同步和阻塞问题.但是消息 ...

  3. passing parameter to an event handler [stackoverflow]

    Q: i want to pass my List<string> as parameter using my event public event EventHandler _newFi ...

  4. Serialization之BinaryFormatter

    前言 BinaryFormatter序列化二进制序列化使用二进制编码来生成精简的序列化,以用于存储或基于套接字的网络流等. 内容 下面通过一个小小的例子来给大家说明什么是BinaryFormatter ...

  5. Tomcat—Bad Request

    前段时间,由于搭建环境的问题,项目暂停了一个多月,终于再次拿起来了,可是历史问题还是没有解决,再次让问题重现了一把. 上面的图片的大概意思就是:错误请求(无效主机名称)     看到这个,我一开始是兴 ...

  6. poj1830(高斯消元解mod2方程组)

    题目链接:http://poj.org/problem?id=1830 题意:中文题诶- 思路:高斯消元解 mod2 方程组 有 n 个变元,根据给出的条件列 n 个方程组,初始状态和终止状态不同的位 ...

  7. [51nod1220] 约数之和(杜教筛+莫比乌斯反演)

    题面 传送门 题解 嗯--还是懒得写了--这里 //minamoto #include<bits/stdc++.h> #define R register #define IT map&l ...

  8. 「产品经理全连接系列2」企业如何开展敏捷或DevOps的研发变革

    大家好,我是华为云的产品经理 恒少: 作为布道师和产品经理,出差各地接触客户是常态,经常和华为云的客户交流.布道.技术沙龙,但是线下交流,覆盖的用户总还是少数. 我希望可以借线上的平台,和用户持续交流 ...

  9. 教主的花园 dp

    题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢333种树,这3 ...

  10. JS模式和原型精解

    首先需要知道的是 模式只是思想.不要用 结构 看模式. ES中函数是对象,因此函数也有属性和方法. 每个函数含有两个属性: length 和 prototype 每个函数含有两个非继承的方法: app ...