HDOJ3068最长回文

最长回文

解法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
复制代码

 

posted on   disppr  阅读(174)  评论(0编辑  收藏  举报

编辑推荐:
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示