华为oj 字符串运用-密码截取
2016-02-29 11:13
441 查看
这道题的主要思路在于确定有两种形式的对称,一种是直接对象比如ABBA,另外一种是中间存在一个对称轴的兑现比如bab.所以对于这两种形式需要分别处理,对每个字符查看当前是否满足对称中间的要求,然后向两边查找最长的值,最后两种情况得到的值进行对比取最大值即可。
#include<iostream> #include<string> using namespace std; void getMax1(string s,int &type1) //对称的中间有值 { int len = s.length(); if(len==1) type1 = 1; else { for(int i=0;i<len;i++) { int j = i-1; int k = i+1; if(j>=0&&k<len&&s[j]==s[k]) { int temp = 1; int left = j-1; int right = k+1; while(left>=0&&right<len) { if(s[left]==s[right]) { ++temp; --left; ++right; } else break; } if(type1<2*temp+1) type1 = 2*temp+1; } } } } void getMax2(string s,int &type2) //直接对称 { int len = s.length(); for(int i=0;i<len;i++) { int j = i+1; if(j<len && s[i] == s[j]) { int temp = 1; int left = i-1; int right = j+1; while(left>=0&&right<len) { if(s[left] == s[right]) { ++temp; --left; ++right; } else break; } if(type2<2*temp) type2 = 2*temp; } } } void getMaxLength(string &s) { int type1 = 0,type2 = 0; getMax1(s,type1); getMax2(s,type2); if(type1>type2) cout<<type1<<endl; else cout<<type2<<endl; } int main() { string s; while(getline(cin,s)) { getMaxLength(s); } return 0; }
相关文章推荐
- Windows下面判断文件是否存在
- Js实现Map对象,set对象
- java 在一段英文文本中计算每个单词出现的次数
- Python爬虫(一):环境配置
- 决定跳槽,整理自己的技术
- JSP_001_jsp入门
- IIS7 应用程序池设置成 经典 v2.0
- HTML5-企业宣传6款免费源码尽在h5edu.cn
- PDF文件的文字如何修改
- android图片压缩总结
- html5篇——新增文档标签
- 解决GoAgent打开https网站SSL证书错误 (安全证书不受信任)
- HorizontalScrollViewQQ侧滑
- 图片轮播+小圆点
- C语言中的基本文件操作:fopen , fread , fseek , fclose.
- wget是一个从网络上自动下载文件的自由工具,支持通过HTTP、HTTPS、FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理
- UVA 10082(p47)----WERTYU
- oracle rac 内核参数详解
- ZZAndroid客户端性能优化----魅族资深工程师毫无保留奉献
- JSON