zoukankan      html  css  js  c++  java
  • 马拉车代码

    /*  gyt
           Live up to every day            */
    #include<cstdio>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<cstring>
    #include<queue>
    #include<set>
    #include<string>
    #include<map>
    #include <time.h>
    #define PI acos(-1)
    using namespace std;
    typedef long long ll;
    typedef double db;
    const int maxn = 1000+10;
    const ll maxm = 1e7;
    const int mod = 1e9+7;
    const double INF = 0x3f3f3f3f;
    const db eps = 1e-9;
    const ll Max=1e19;
    char str[maxn];
    char tmp[maxn<<1];
    int Len[maxn<<1];
    int lenyuan, len;
    string anss;
    
    bool safe(char c) {
         if (c!='A'&&c!='H'&&c!='I'&&c!='M'&&c!='O'&&c!='T'&&c!='U'&&c!='V'&&c!='W'&&c!='X'&&c!='Y') return 0;
         return 1;
    }
    void init() {
        tmp[0]='@';
        for (int i=1; i<=2*lenyuan; i+=2) {
            tmp[i]='#';  tmp[i+1]=str[i/2];
        }
        tmp[2*lenyuan+1]='#';
        tmp[2*lenyuan+2]='$';
        tmp[2*lenyuan+3]=0;
    }
    int manacher() {
        int mx=0, ans=0, po=0, reslen=0, resCenter=0;
        for (int i=1; i<len; i++) {
            if (mx>i)  Len[i]=min(mx-i, Len[2*po-i]);
            else  Len[i]=1;
            while(tmp[i-Len[i]]==tmp[i+Len[i]]) {
                Len[i]++;
            }
            if (Len[i]+i>mx) {
                mx=Len[i]+i;  po=i;
            }
            if (reslen<Len[i]) {
                reslen=Len[i];
                resCenter=i;
            }
            ans = max(ans, Len[i]);
        }
        string aa;  aa.clear();
        for (int i=0; i<lenyuan; i++)  {
            aa+=str[i];
        }
        anss=aa.substr((resCenter - reslen)/2, reslen - 1);
        return (ans-1);
    }
    void solve() {
        memset(str, 0, sizeof(str));
        memset(tmp, 0, sizeof(tmp));
        scanf("%s", str);
        memset(Len, 0, sizeof(Len));
        lenyuan=strlen(str);
        init();
        len=2*lenyuan+1;
        int ans=manacher();
        printf("%d
    ", ans);
        cout << ans << endl;
    }
    int main() {
        int t=1;
        freopen("in.txt", "r", stdin);
        scanf("%d", &t);
        for (int T=1; T<=t; T++) {
            solve();
        }
    }

      len[i]-1表示以i为中心,回文串的长度

  • 相关阅读:
    七种常见的回归分析—转载
    Python模块之 __future__ 转载
    Java 快速排序
    在给定范围内产生指定个数不重复的随机数
    Java 冒泡排序
    jquery文本框效果
    jquery复选框
    struts2下的Ajax
    java线程系列---Runnable和Thread的区别
    System.getProperty("user.dir")
  • 原文地址:https://www.cnblogs.com/gggyt/p/7191450.html
Copyright © 2011-2022 走看看