POJ3974 [Manacher]

Palindrome
Description
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, “Can you propose an efficient algorithm to find the length of the largest palindrome in a string?”
A string is said to be a palindrome if it reads the same both forwards and backwards, for example “madam” is a palindrome while “acm” is not.
The students recognized that this is a classical problem but couldn’t come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said “Okay, I’ve a better algorithm” and before he starts to explain his idea he stopped for a moment and then said “Well, I’ve an even better algorithm!”.
If you think you know Andy’s final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string “END” (quotes for clarity).
Output
For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6

就是求最长回文子串。 KMP,AC自动机什么的其实都可以,有一种专门求最长回文子串的算法。就是Manacher!

首先,对于一个回文子串,奇偶数是要讨论的。这个算法在处理时就在每个字符加入一个字符,使其变成一个奇数个数的串。比如:abaaabbaabbababb 就变成了 $a#b#a#a#a#b#b#a#a#b#b#a#b#a#b#b\0
前后两个字符防止越界,代码如下:

    str[0] = '$';
    str[1] = '#';
    for(int i = 0; i < l; i++) {
      str[len++] = s[i];
      str[len++] = '#';
    }
    str[len] = '\0';

我们用p[i]来表示以i为回文子串中心的最长回文子串的长度。接下来就是按照回文子串的性质DP
先上代码:

    for(int i = 0; i < len; i++) {
      if (mx > i) p[i] = Min(p[id * 2 - i], mx - i);
      else p[i] = 1;
      for(; str[i - p[i]] == str[i + p[i]]; p[i]++);
      if (i + p[i] > mx) {
        mx = i + p[i];
        id = i;
      }
      ans = Max(p[i], ans);
    }

这里出现了一个 mx 和 id 。它们有什么用呢?我们用 mx 来存储在之前求到的回文子串中延伸到最右边的地方。 id 来存储相应 mx 的中心。
现在着重看一个地方:

      if (mx > i) p[i] = Min(p[id * 2 - i], mx - i);

就是这个步骤可以减少很多复杂度。这个为什么成立呢?来看一张图吧:
这里写图片描述
该图来源:(http://blog.csdn.net/yzl_rex/article/details/7908259) 万分感谢
很显然 id * 2 - i 就是 i 关于 id 的对称点。那么我们可以得到的是以 j 为中心的回文子串的长度。这里为什么是 Min 呢。我们先看对这个 p[i] 有哪些限制条件吧。
首先 p[i] <= mx - i ,因为如果 i 的回文子串可以延伸到 mx 之外的话,我们会发现超出的那一部分,是和其与id对称的位置的子串不对称的(因为如果对称mx还可以继续延伸)。而 str[id..j] == str[id..i],所以 p[i] <= mx - i。
另外 p[i] <= p[j] 这很显然,因为既然不能延伸到 mx 之外,那么再这么一整段之中都是与 j 对称的。
剩下来的一部分就是硬算了。

      else p[i] = 1;
      for(; str[i - p[i]] == str[i + p[i]]; p[i]++);
      if (i + p[i] > mx) {
        mx = i + p[i];
        id = i;
      } // 更改 mx 和 id
      ans = Max(p[i], ans);
    }

最后贴上这道题的代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#define N 1000010
using namespace std;

char s[N], str[N << 1];
int p[N << 1], Case = 0;

inline int Max(int a, int b) {
  static int mask; mask = (b - a) >> 31;
  return (a & mask) | (b & ~mask);
}
inline int Min(int a, int b) {
  static int mask; mask = (a - b) >> 31;
  return (a & mask) | (b & ~mask);
}

int main(void) {
  while(~scanf("%s", s)) {
    if (!strcmp(s, "END")) break;
    int l = strlen(s), len = 2, id = 0, mx = 0, ans = 1;
    str[0] = '$';
    str[1] = '#';
    for (int i = 0; i < l; i++) {
      str[len++] = s[i];
      str[len++] = '#';
    }
    str[len] = '\0';
    for (int i = 0; i < len; i++) {
      if (mx > i) p[i] = Min(p[id * 2 - i], mx - i);
      else p[i] = 1;
      for (; str[i - p[i]] == str[i + p[i]]; p[i]++);
      if (i + p[i] > mx) {
        mx = i + p[i];
        id = i;
      }
      ans = Max(p[i], ans);
    }
    printf("Case %d: %d\n", ++Case, ans - 1);
  }
  return 0;
}

总结:
字符串的题目:KMP,Manacher,SuffixArray,AhoCorasickAutomata等算法其实都是找到了字符串的一些特殊性质,能够大量减少复杂度,这是一种非常好的思考方式,今后如果再遇到字符串匹配等问题,可以多从这方面考虑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值