首页 > 其他 > 详细

HDOJ3068最长回文

时间:2016-07-04 21:56:47      阅读:255      评论:0      收藏:0      [点我收藏+]

最长回文

解法1、manacher算法

技术分享
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
char str[1000002 + 1200];

int fast(char *p) {
    int ans = 1;
    for (int i = 1; p[i]; ++i) {
        int s = i, e = i, t;
        while (p[e + 1] == p[i]) ++e;
        i = e;
        while (p[s - 1] == p[e + 1]) --s, ++e;
        if ((t = e - s + 1) > ans) ans = t;
    }
    return ans;
}

int main() {
    str[0]=$;

    while(scanf("%s", str+1) !=EOF) {
        printf("%d\n", fast(str));
    }
    return 0;
}
View Code

 

解法2、后缀树

技术分享
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
char str[1000002 + 1200];

int fast(char *p) {
    int ans = 1;
    for (int i = 1; p[i]; ++i) {
        int s = i, e = i, t;
        while (p[e + 1] == p[i]) ++e;
        i = e;
        while (p[s - 1] == p[e + 1]) --s, ++e;
        if ((t = e - s + 1) > ans) ans = t;
    }
    return ans;
}

int main() {
    str[0]=$;

    while(scanf("%s", str+1) !=EOF) {
        printf("%d\n", fast(str));
    }
    return 0;
}
View Code

 

HDOJ3068最长回文

原文:http://www.cnblogs.com/cshg/p/5641755.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!