求调
查看原帖
求调
1378937
CN_Huang楼主2025/2/6 18:14
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define int long long
#define N 500010
using namespace std;
char s[N], s1[N];
int len=0, len1=0;
int ans=-100, tot=0, last=0;
int a[N], P[N];
struct tree{
   int father, len, cnt;
   int son[26];
}t[N<<1];
void manacher(){
   int id=0, mx=0;
   for(int i=0; i<len1; i++){
      if(mx>i) P[i] = min(P[id*2-i], mx-i); else P[i]=1;
	  while(s1[i+P[i]]==s1[i-P[i]]) ++P[i];
	  if(i+P[i]>mx) mx = i+P[id=i];
   }
}
void NewNode(int len){
   t[++tot].len = len;
   t[tot].father = -1;
   t[tot].cnt = 1;
   memset(t[tot].son, 0, sizeof(t[tot].son));
}
void init(){
   last=0; tot=-1;
   NewNode(0);
}
void Insert(int c){
   NewNode(t[last].len+1);
   memset(t[tot].son, 0, sizeof(t[tot].son));
   int p=last, np=tot;
   while(p!=-1 && !t[p].son[c])
      t[p].son[c]=np, p=t[p].father;
   if(p==-1) t[np].father = 0;
   else{
      int q = t[p].son[c];
      if(t[q].len = t[p].len+1) t[np].father = q;
	  else{
	     NewNode(t[p].len+1);
		 int nq = tot;
		 for(int i=0; i<=25; i++)
		    t[nq].son[i] = t[q].son[i];
		 t[nq].father = t[q].father;
		 while(q!=-1 && t[p].son[c]==q)
		    t[p].son[c]=nq, p=t[p].father;
	     t[q].father = t[np].father = nq;
	  }
   }
   last = np;
}
void dfs(int start, int end){
   if(s1[start]=='#'||s1[start]=='^') start++;
   if(s1[end]=='#'||s1[end]=='^') end--;
   int now = start, p=0;
   while(now<=end){
      p = t[p].son[s1[now]-'a'];
	  now+=2;
   }
   ans = max(ans, t[p].cnt*(end-start+2)/2);
}
signed main()
{
   scanf("%s", s); len=strlen(s);
   for(int i=0; s[i]; i++)
      s1[len1++]='#', s1[len1++]=s[i];
   s1[len1++]='^';
   manacher();
   init();
   for(int i=0; i<len; i++)
      Insert(s[i]-'a');
   for(int i=tot; i>=1; i--)
      t[t[i].father].cnt += t[i].cnt;
   for(int i=0; i<len1; i++){cout<<P[i]<<" "<<s1[i]<<endl;
      dfs(i-P[i]+1, i+P[i]-1);
   }
   printf("%lld ", ans);
   return 0;
}

此代码的测试信息

2025/2/6 18:14
加载中...