1040. Longest Symmetric String (25)

1.求最长回文子字符串。

2.getline输入数据。

3.最好使用manacher方法。

4.暴力方法解决不会超时。

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

 
暴力AC代码:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

int main(void)
{

	string  str;
	getline(cin,str);
	int maxLen = 0;
	for (int i = 0; i<str.size(); i++)
	{
		int oddLen = 1, evenLen = 0;
		for (int j = 1; i - j >= 0 && i + j<str.size(); j++)
		{
			if (str[i + j] == str[i - j])
				oddLen += 2;
			else break;
		}
		for (int j = 0; i - j >= 0 && i + j + 1<str.size(); j++)
		{
			if (str[i + j + 1] == str[i - j])
				evenLen += 2;
			else break;
		}
		maxLen = max(maxLen, max(oddLen, evenLen));
	}
	cout << maxLen << endl;
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注