L2-008. 最长对称子串
来源:互联网 发布:淘宝京润珍珠是真的吗 编辑:程序博客网 时间:2024/06/16 07:19
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定”Is PAT&TAP symmetric?”,最长对称子串为”s PAT&TAP s”,于是你应该输出11。
输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
分析:题目很好理解,做起来可能有些细节要注意,一个字符或者没有对称的字串输出都是1,其他的分两种情况奇数还是偶数。(用三重循环其实是不用考虑奇偶的,但是会超时~)
#include<iostream>#include<string>using namespace std;int main(void){string str;int max1=1; getline(cin,str); for(int i=0;i<str.length();i++) { int t=1,m=1,flag1=0,flag2=0; //条件判断注意加和减的时候不能超出了字符串的范围,否则后果很严重 while(str[i+t]==str[i-t]&&i-t>=0&&i+t<str.length()) { t++; flag1=1; } while(str[i-m+1]==str[i+m]&&i-m+1>=0&&i+m<str.length()) {m++; flag2=1; } if(flag1==1&&2*(t-1)+1>max1) max1=2*(t-1)+1; if(flag2==1&&2*(m-1)>max1) max1=2*(m-1); }cout<<max1<<endl;return 0;}
0 0
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- GPLT L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- 到底是不是太胖了(卡精度)
- while函数输入
- 主流浏览器内核介绍
- 网易编程题七,买苹果(计算)
- Android开发中的一些边缘技巧
- L2-008. 最长对称子串
- runtime获取属性列表,修改属性值,添加并修改私有方法的调用
- 命名管道(FIFO)
- 数据存储方式和存储位置
- 扩展KMP
- 移动端知识的一些总结
- 语音通知
- 只能输入带正负号的整数和小数的正则表达式
- 单片机系列(定时器)-2