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


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

代码如下:

#include 
#include 

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
#include
#include

#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 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\n", max/2);
	}
	return 0;
}


转载于:https://www.cnblogs.com/aiwz/p/6154146.html

你可能感兴趣的:(1040. Longest Symmetric String (25))