zoukankan      html  css  js  c++  java
  • L2-008 manacher 的应用

      附上题目链接:https://www.patest.cn/contests/gplt/L2-008

      

    对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s",于是你应该输出11。

    输入格式:

    输入在一行中给出长度不超过1000的非空字符串。

    输出格式:

    在一行中输出最长对称子串的长度。

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    const int maxn = 3*1000 + 100;
    char s[maxn];
    char a[maxn];
    int p[maxn];
    
    void manacher(char *s) {
        int len = strlen(s+1);
        int m = 2*len + 1;
        for(int i=1; i<=len; i++) {
            a[i<<1] = s[i];
            a[i<<1|1] = '#';
        }
        a[0] = '+'; a[1] = '#'; a[m+1] = '-';
        int mx = 0, idx;
        for(int i=1; i<=m; i++) {
            if(mx > i) p[i] = min(p[2*idx-i], mx-i);
            else p[i] = 1;
            for(; a[i-p[i]]==a[i+p[i]]; p[i]++);
            if(p[i]+i>mx) mx=p[i]+i, idx = i;
        }
    }
    
    int main() {
        gets(s+1);
        manacher(s);
        int res = 0;
        int len = strlen(s+1);
        for(int i=1; i<=2*len+1; i++) {
            res = max(res, p[i]);
        }
        printf("%d
    ", res-1);
        return 0;
    }
  • 相关阅读:
    java核心技术记录之集合
    Set的非重复判断是根据什么判断的
    struts2-2.3.4.1的struts-default.xml源码
    纯代码搭建项目框架
    初始化项目
    项目部署
    使用estimatedRowHeight的优缺点
    懒加载
    闭包
    Swift 类的构造函数
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5557861.html
Copyright © 2011-2022 走看看