1040 Longest Symmetric String

原创 2015年11月21日 10:11:45

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(){
	for(string str;getline(cin,str);){
		int max_length = 1;
		for(int i = 0; i < str.length();i++){
			int length1 = 1;
			int length2 = 1;
			//判断长度为偶数的情况
			int index_1 = i;
			int index_2 = i+1;
			if(index_1 < 0 || index_2 >= str.length()){
				continue;
			}
			if(str[index_1] == str[index_2]){
				length1 = 2;
				while(1){
					index_1--;
					index_2++;
					if(index_1 < 0  || index_2 >= str.length()){
						break;
					}
					if(str[index_1] == str[index_2]){
						length1+=2;
					}else{
						break;
					}
				}
			}
			//判断长度为奇数的情况
			index_1 = i-1;
			index_2 = i+1;
			if(index_1 < 0 || index_2 >= str.length()){
				continue;
			}
			if(str[index_1] == str[index_2]){
				length2 = 3;
				while(1){
					index_1--;
					index_2++;
					if(index_1 < 0 || index_2 >= str.length()){
						break;
					}
					if(str[index_1] == str[index_2]){
						length2+= 2;
					}else{
						break;
					}
				}
			}
			if(length1 > max_length){
				max_length = length1;
			}
			if(length2 > max_length){
				max_length = length2;
			}
		}
		cout<<max_length<<endl;
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

1040. Longest Symmetric String

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1040 暴力 // 暴力破解 // 分类讨论 #include #include #in...

1040 Longest Symmetric String

Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

1040.Longest Symmetric String

【题意】         找出给的字符串中最长对称子串的长度 【思路】         对长度分奇偶数分别进行二分查找,对于每个查找的长度判断是否存在该长度的对称子串,若存在则更新'left'指针.....

1040. Longest Symmetric String (25)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp...

1040. Longest Symmetric String (25)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp...

1040. Longest Symmetric String (25)

题目要求判断最长的回文,有两种思路可供选择。 思路一,从两头进行判断,定义两个指针start_index和end_index分别指向头部和尾部,首先固定start_index,让end_index从最...

1040. Longest Symmetric String (25)

题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) ...

1040. Longest Symmetric String (25)

发现以前做过了; 求最长的回文串长度;就是吧所有的可能都求一下,得出最长,这里利用了“/”取模特性,把奇数和偶数的情况一起讨论了 联动以前做的友缘函数模版或者看后面贴的

1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...

1040. Longest Symmetric String (25)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)