PAT 1040. Longest Symmetric String (25)
2015-08-17 16:47
519 查看
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 <iostream> #include <string> #include <algorithm> #include <vector> #include <map> #include <set> #include <cstring> using namespace std; int main(void) { string s; getline(cin,s); int size=s.size(),max=0; if(size==1) { cout<<1; return 0; } for(int i=0;i<size;i++) { int j=1,count=1; while(i-j>=0&&i+j<size) { if(s[i+j]==s[i-j]) { count+=2; j++; } else break; } if(count>max) max=count; j=0; count=0; while(i-j>=0&&i+j+1<size) { if(s[i-j]==s[i+j+1]) { count+=2; j++; } else break; } if(count>max) max=count; } cout<<max; }
相关文章推荐
- java.lang.OutOfMemoryError: bitmap size exceeds VM budget
- 基于hadoop1.2.1的hbase伪分布式安装
- 2015.8.17感悟
- Frege-基于JVM的类Haskell纯函数式编程语言
- 反射获取指定类型
- 卷积和池化的区别
- 08基本运算符
- getopt_long使用
- 分布式文件系统FastDFS原理介绍
- Emmet的高级功能与使用技巧
- 用php怎么改文件名
- Unix IPC之Posix信号量实现生产者消费者
- 软件系统上线前演示剧本
- Win10更改高分屏高DPI字体还是模糊的两种解决办法
- HDU 2680:Choose the best route【Dijkstra & SPFA】
- zoj 3537 Cake(区间dp)
- zf-关于邵阳市打印模块个别单号打印之后不会跳转到收费模块的BUG的解决方法
- 07基本数据类型
- 字符串匹配:KMP
- post a json object with an array using curl