码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ3068最长回文

时间:2016-07-04 21:59:15      阅读:216      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!