首页 > 代码库 > 马拉车代码

马拉车代码

/*  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\n", 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();
    }
}

 

马拉车代码