PAT 1040. Longest Symmetric String (25)
2015-08-11 14:08
471 查看
1040. Longest Symmetric String (25)
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> using namespace std; int main() { string s; getline(cin, s); int maxlength = 0; for (int i = 0; i < 2 * s.size() - 1; i++) { if (2 * s.size() - i - 1 <= maxlength) break; int max=0; if (i % 2 == 0) { int center = i / 2; ++max; int j = 1; while (center - j >= 0 && center + j < s.size()) { if (s[center - j] == s[center + j]) max += 2; else break; j++; } } else { int left = (i - 1) / 2; int right = (i + 1) / 2; while (left >= 0 && right < s.size()) { if (s[left] == s[right]) max += 2; else break; left--; right++; } } if (max > maxlength) maxlength = max; } cout << maxlength; }
相关文章推荐
- 还是畅通工程
- Android开源项目xUtils HttpUtils模块分析
- 【机器学习算法实现】主成分分析(PCA)——基于python+numpy
- 几个常见的压缩算法
- ORA-12519 TNS no appropriate service handler found
- PAT 1039. Course List for Student (25)
- [转]深入理解Java Proxy机制
- 继续畅通工程
- ArcGIS教程:“流向”的工作原理
- VIM的常用命令
- Leetcode#7||Reverse Integer
- OGG-01453 Database login information not specified in parameter file
- 杭电ACM2043java做法
- 利用赛门铁克Symantec Endpoint Protection漏洞渗透企业网络
- IOC/AOP随笔目录
- HDU 4240 --Route Redundancy【最大流dinic && DFS】
- 此次文章,讲述的是导航控制器全屏滑动返回效果(抽屉式导航),而且代码量非常少,10行内搞定。
- HTML5应用程序缓存Application Cache
- PHP站内搜索:多关键字、加亮显示
- 继续畅通工程