HDU - 3068 最长回文(manacher)

来源:互联网 发布:js数组常用方法 编辑:程序博客网 时间:2024/04/30 23:22

题目大意:中文题

解题思路:manacher裸题了

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N = 220010;char s[N];int p[N];int len;void init() {    len = strlen(s);    for (int i = len; i >= 0; i--) {        s[i * 2 + 2] = s[i];        s[i * 2 + 1] = '#';    }    s[0] = '$';}void solve() {    int id = 0, ans = 0, mx = 0;    for (int i = 2; i < 2 * len + 1; i++) {        if (mx > i) p[i] = min(p[2 * id - i], mx - i);        else p[i] = 1;        while (s[i - p[i]] == s[i + p[i]]) p[i]++;        if (i + p[i] >= mx) {            id = i;            mx = i + p[i];        }        if (p[i] > ans) ans = p[i];    }    printf("%d\n", ans - 1);}int main() {    while (scanf("%s", s) != EOF) {        init();        solve();    }    return 0;}
0 0
原创粉丝点击