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. asp.net core tags 扩展之 id 和 name

    asp.net core 页面 TagHelper  的 Id 和 Name 属性扩展 . [HtmlTargetElement(Attributes = "asp-name")] ...

  2. 跨平台技术iOS与安卓

    1.教学资源获取 Flutter的使用教学笔记 2.本地学习笔记

  3. 【bzoj3209】: 花神的数论题 数论-DP

    [bzoj3209]: 花神的数论题 首先二进制数中1的个数最多就是64个 设所有<=n的数里二进制中1的个数为i的有a[i]个 那么答案就是  然后快速幂 求a[i]可以用DP 设在二进制中从 ...

  4. 【转】使用母版页时内容页如何使用css和javascript

    源地址:https://www.cnblogs.com/accumulater/p/6767138.html

  5. nginx的worker_processes优化

    nginx的worker_processes参数来源: http://bbs.linuxtone.org/thread-1062-1-1.html分享一:搜索到原作者的话:As a general r ...

  6. 最优的cuda线程配置

    1 每个SM上面失少要有192个激活线程,寄存器写后读的数据依赖才能被掩盖   2 将 寄存器 的bank冲突降到最低,应尽量使每个block含有的线程数是64的倍数   3 block的数量应设置得 ...

  7. [翻译]CURAND Libaray--Host API--(2)

    Translated by xingoo 如果有错误请联系:xinghl90@gmail.com 2.3 返回值 所有的CURAND host端的函数返回值都是curandStatus_t.如果调用没 ...

  8. P2597 [ZJOI2012]灾难

    \(\color{#0066ff}{ 题目描述 }\) 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝, ...

  9. EA添加流程图

  10. pytorch 加载mnist数据集报错not gzip file

    利用pytorch加载mnist数据集的代码如下 import torchvision import torchvision.transforms as transforms from torch.u ...