参考分析HDU 3068 最长回文串

废话就不多说了,开始。。。

    标题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068

    分析:参考manacher算法http://wenku.baidu.com/view/3031d2d3360cba1aa811da42.html

    

    每日一道理
航行者把树比作指引方向的路灯,劳动者把树比作遮风挡雨的雨伞,诗人把树比作笔下的精灵,而我却要把树比作教师,它就是为我们遮风挡雨的伞,指明方向的路灯,打开知识殿堂的金钥匙。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn = 110000 + 10;
char str[maxn];
char str1[maxn*2];
int p[maxn*2];
int n;
void manacher() {
    memset(p,0,sizeof(p));
    int MaxId=-1;
    int id;
    for(int i=1; str1[i]; ++i) {
        if(MaxId>i) p[i]=min(p[2*id-i], MaxId-i);
        else p[i]=1;
        while(str1[i+p[i]]==str1[i-p[i]])p[i]++;
        if(p[i]+i>MaxId)
            MaxId=p[i]+i, id=i;
    }
}
void change() {
    str1[0]='$';
    str1[1]='#';
    for(int i=0;i<n; ++i) {
        str1[i*2+2]=str[i];
        str1[i*2+3]='#';
    }
}
int main() {
    while(scanf ("%s", str) != EOF) {
        memset(str1, '\0', sizeof(str1));
        n=strlen(str);
        change();
        manacher();
        int ma = -1;
        for(int i = 0; str1[i]; ++i)
            if(p[i]-1>ma)ma=p[i]-1;
        printf ("%d\n", ma);
    }
    return 0;
}

    

    

文章结束给大家分享下程序员的一些笑话语录: 联想——对内高价,补贴对外倾销的伟大“民族”企业。

posted @ 2013-05-12 16:53  坚固66  阅读(122)  评论(0编辑  收藏  举报