1040. Longest Symmetric String (25)

题目如下:

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


题目要求判断最长的回文,有两种思路可供选择。

思路一,从两头进行判断,定义两个指针start_index和end_index分别指向头部和尾部,首先固定start_index,让end_index从最后一个元素向前遍历,直到碰到start_index,其间对start_index到end_index的范围进行回文判断,回文判断的规则很简单,如果start和end指向的元素一样,回文长度length=2,然后start+1,end-1,继续比较,如果符合则继续+2,直到start<end不再满足,注意在这之中只要有一次不符合start指向的元素不等于end指向的元素,累加的长度length都是无效的,都应当输出1。注意一种情况,如果形如abba这样的形式,跳出循环时start = 2、end = 1,length = 4满足条件;但是如果是形如aba这样的形式,start=end=1,length=2,不能得到正确结果,这时候应当判断是否start==end并且length!=1,则说明是回文、且需要修正,这时候把length+1即可得到正确结果。


注意一个问题,为了能输入空格,使用getline(cin,str)来输入字符串。

代码如下:

#include <iostream>
#include <string>

using namespace std;

string str;

int isRevese(int s, int e){
    int length = 1;
    while(s < e){
        if(str[s] != str[e]) return 1;
        s++;
        e--;
        if(length == 1) length = 2;
        else length += 2;
    }

    if(length != 1 && s == e) length++;

    return length;

}

int main()
{
    getline(cin,str);

    int start_index = 0;
    int end_index = str.length() - 1;
    int max_length = -1;
    int len = 0;

    for(start_index = 0; start_index < str.length(); start_index++){
        for(end_index = str.length() - 1; end_index >= start_index; end_index--){
            len = isRevese(start_index,end_index);
            if(len > max_length){
                max_length = len;
            }
        }
    }
    cout << max_length << endl;
    return 0;
}

思路二,采用中心枚举法,以每个字符为中心向两端枚举,这个方法最大的问题在于枚举时对于形如abba的处理十分棘手,因为无论列举哪个b,都不能找到以b为中心的回文,而事实上abba就是回文。

这里我看到了sunbaigui的巧妙解法,他把所有的字符前面都加一个前导-1,这样通过以-1为中心枚举就可以得到正确的回文了,例如abba变为-1a-1b-1b-1a,通过中间的-1,得到了整个序列,这时候的回文长度是正确长度的2倍,只需要除以2即可得到正确结果。

这里直接贴的是sunbaigui的代码,欢迎去到他的博客。

#include<iostream>
#include<string.h>
#include<vector>

#define Max 10000
int dp[2*Max+1];
char str[Max];

int mmax(int a, int b)
{
	if(a > b) return a;
	else return b;
}
int main()
{
	while(gets(str))
	{
		//memset(dp, -1, sizeof(dp));
		int len = strlen(str);
		//insert special character into str, must not appeared in str
		std::vector<int> magic;
		for(int i = 0; i < len; ++i)
		{
			magic.push_back(-1);//special character
			magic.push_back(str[i]);//character to int
		}
		magic.push_back(-1);
		//enumerate center point for magic vector
		len = (int)magic.size();
		int max = 1;
		for(int i = 1; i < len; ++i)
		{
			int l, r;
			int step = 1;
			for(l = i-1, r = i+1; l >= 0 && r < len; l--, r++)
			{
				if(magic[l] != magic[r])
					break;
				step += 2;
			}
			max = mmax(max, step);
		}

		printf("%d
", max/2);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/aiwz/p/6154146.html