码迷,mamicode.com
首页 > 其他好文 > 详细

1040. Longest Symmetric String (25)

时间:2017-10-27 17:56:31      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:通过   file   回文串   ret   case   get   char   i++   char s   

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

解题思路:枚举,但需要把输入的字符串进行填充,abba不能通过枚举来判断为回文串,但可以通过填充,例如-1a-1b-1b-1a-1,此时就不会出现abba不是回文串了。

#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
	vector<char>str;
	char s[1002];
	gets(s);
	int length=strlen(s);
	int i,j;
	for(i=0;i<length;i++){
		str.push_back(-1);
		str.push_back(s[i]);
	}
	str.push_back(-1);
	int size=str.size();
	int max=0;
	for(i=1;i<size;i++){
		int l,r;
		int length=0;
		for(l=i-1,r=i+1;l>=0&&r<size;l--,r++){
			if(str[l]!=str[r]){
				break;
			}
			length+=2;
		}
		max=max>length?max:length;
	}
	printf("%d\n",max/2);
	return 0;
}

  

1040. Longest Symmetric String (25)

标签:通过   file   回文串   ret   case   get   char   i++   char s   

原文地址:http://www.cnblogs.com/grglym/p/7744128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!