zoukankan      html  css  js  c++  java
  • HDU 3068 O(n)最长回文字符串

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <cctype>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <map>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <set>
    #define X first
    #define Y second
    #define sqr(x) (x)*(x)
    #pragma comment(linker,"/STACK:102400000,102400000")
    using namespace std;
    const double PI = acos(-1.0);
    map<int, int>::iterator it;
    typedef long long LL ;
    template<typename T> void checkmin(T &x, T y) {x = min(x, y);}
    template<typename T> void checkmax(T &x, T y) {x = max(x, y);}
    
    const int N = 110005 * 2;
    int p[N];
    void pk(const char *s) {
        int n = strlen(s);
        int i;
        int mx = 0;
        int id;
        for(i = 1; i < n; i++) {
            if(mx > i)
                p[i] = min(p[2*id-i],  mx - i);
            else
                p[i] = 1;
            while(s[i+p[i]] == s[i-p[i]]) ++p[i];
            if(p[i] + i > mx) {
                mx = p[i] + i;
                id = i;
            }
        }
    }
    
    void init(char *s) {
        int n = strlen(s);
        for(int i = n - 1; i >= 0; --i) {
            s[2*i+2] = s[i];
            s[2*i+3] = '#';
        }
        s[0] = '$';
        s[1] = '#';
        s[2*n+2] = '';
        //printf("%s
    ", s);
    }
    char s[N];
    int main() {
        //freopen("Hin.txt","r",stdin);
        while(~scanf("%s", s)) {
            init(s);
            pk(s);
            int n = strlen(s);
            int res = 0;
            for(int i = 1; i < n; ++i) {
                res = max(res,p[i]);
            }
            printf("%d
    ", res - 1);
        }
        return 0;
    }
  • 相关阅读:
    ES6对象的扩展
    ES6函数的扩展
    ES6新增变量
    ES6框架的搭建
    自适应布局 左右结构、上下结构
    iframe 子页面改变父页面样式
    检测终端类型
    $.grep()
    点击元素内部不隐藏,点击元素外部元素隐藏
    angular表单验证
  • 原文地址:https://www.cnblogs.com/cxw199204/p/3348506.html
Copyright © 2011-2022 走看看