1040. Longest Symmetric String (25)
2016-04-26 09:12
302 查看
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
IDEA
1.遍历每个字符,以它为中心,判断:
若回文为奇数个数,则判断s[i] 两侧字符是否相等
若回文为偶数个数,则判断s[i]与右侧字符是否相等
CODE
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
IDEA
1.遍历每个字符,以它为中心,判断:
若回文为奇数个数,则判断s[i] 两侧字符是否相等
若回文为偶数个数,则判断s[i]与右侧字符是否相等
CODE
#include<iostream> #include<cstring> using namespace std; int main(){ string s; getline(cin,s); int max=1; for(int i=0;i<s.size();i++){ int p=i-1,q=i+1; while(p>=0&&q<s.size()){//回文是奇数个 if(s[p]==s[q]){ if(max<q-p+1){ max=q-p+1; } p--; q++; }else{ break; } } } for(int i=0;i<s.size();i++){//回文是偶数 int p=i,q=i+1; while(p>=0&&q<s.size()){ if(s[p]==s[q]){ if(max<q-p+1){ max=q-p+1; } p--; q++; }else{ break; } } } cout<<max; return 0; }
相关文章推荐
- SSL证书生成方法
- 深度学习--学习总结
- 满足lisheng需求 linux添加环境变量 linux批处理 2016.04.25小结
- Ubuntu挂起后无法唤醒的问题以及解决方式
- 【练习】登录界面
- centos 64位 下hadoop-2.7.2 下编译
- 用户接口(User exit)
- Android Studio安装与SDK配置
- asp.net简单生成XML文件的方法
- oraclelinux系统udev配置
- 多臂赌博机,multi-armed bandit problem(1):
- 代码片断
- 针对网站的UI分析
- web.config中customErrors与httpErrors的区别
- POJ 3096 Surprising Strings(STL map)
- G. 易彰彪的一张表(kMP)
- 楼梯问题
- ubuntu14.04安装squirrelmail
- 逛了一下,找到两个自己感兴趣的
- 大脑“芯片”与地平线机器人科技创新企业