QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#253102 | #801. 回文自动机 | Mobius_127 | 0 | 14ms | 63152kb | C++14 | 2.7kb | 2023-11-16 17:57:00 | 2023-11-16 17:57:01 |
Judging History
answer
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cctype>
#include <vector>
#include <queue>
#include <bitset>
#define mem(arr, x) memset(arr, x, sizeof(arr))
#define mcy(arr, b) memcpy(arr, b, sizeof(arr))
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
typedef long long ll;
typedef pair <int, int> Pii;
const int INF=0x3f3f3f3f;
const int cp=998244353;
inline int mod(int x){return x+(x<0?cp:0)-(x>=cp?cp:0);}
inline void plust(int &x, int y){x=mod(x+y);return ;}
inline void minut(int &x, int y){x=mod(x-y);return ;}
inline int read(){
char ch=getchar();int x=0, f=1;
while(!isdigit(ch)){if(ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
inline void write(int x){
if(x<0) putchar('-'), x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline int ksm(int a, int b=cp-2){
int ret=1;
for(; b; b>>=1, a=1ll*a*a%cp)
if(b&1) ret=1ll*ret*a%cp;
return ret;
}
const int N=1e6+6;
namespace SAM{
const int M=N<<1;
int ch[M][26], fa[M], len[M], occ[M], lst, ndc;
inline int clr(int x){fa[x]=len[x]=occ[x]=0, mem(ch[x], 0);return x;}
inline void rmk(){clr(ndc=lst=1);}
inline int insert(char c){
int p=lst, cur=clr(++ndc), cc=c-'a';len[cur]=len[p]+1;
for(; p&&!ch[p][cc]; p=fa[p]) ch[p][cc]=cur;int q=ch[p][cc];
if(!q) fa[cur]=1;else if(len[q]==len[p]+1) fa[cur]=q;
else{
int nex=clr(++ndc);mcy(ch[nex], ch[q]);len[nex]=len[p]+1;fa[nex]=fa[q];
for(; p&&ch[p][cc]==q; p=fa[p]) ch[p][cc]=nex;fa[q]=fa[cur]=nex;
}return lst=cur;
}
int pos[N], f[M][25];vi G[M];
void dfs(int x){
for(int i=1; i<25; ++i) f[x][i]=f[f[x][i-1]][i-1];
for(auto v:G[x]) f[v][0]=x, dfs(v), occ[x]+=occ[v];
}
inline void ins(char *str, int m){
rmk();for(int i=1; i<=m; ++i) pos[i]=insert(str[i]), ++occ[pos[i]];
for(int i=2; i<=ndc; ++i) G[fa[i]].pb(i);dfs(1);
}
inline int find(int l, int r){
int u=pos[r];for(int i=24; i>=0; --i)
if(len[f[u][i]]>=r-l+1) u=f[u][i];
return u;
}
inline int OCC(int l, int r){return occ[find(l, r)];}
}
int n, p[N];char s[N], t[N<<1];ll ans;
inline void chk(int mid, int len){
int l=mid-len+1>>1, r=mid+len>>1;//printf("find [%d %d]*%d\n", l, r, SAM :: OCC(l, r));
ans=max(ans, 1LL*SAM :: OCC(l, r)*(r-l+1)*(r-l+1));
}
signed main(){
scanf("%s", s+1);n=strlen(s+1);SAM :: ins(s, n);
t[0]='~', t[1]='#';for(int i=1; i<=n; ++i) t[i<<1]=s[i], t[i<<1|1]='#';
for(int i=1, mid=0, R=0; i<=n+n; ++i){
if(i<R) p[i]=min(R, p[mid+mid-i]);
while(t[i-p[i]-1]==t[i+p[i]+1]) ++p[i], chk(i, p[i]);
if(R<i+p[i]) mid=i, R=i+p[i];
}
printf("%lld\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 14ms
memory: 63152kb
input:
bdgedcfadbabbfgeacdgbggaefddebabbfgeacdgbefaecfddffeddacbabfcgecedacbffeddacbabfebadggfafabcdfdeaabdeecgbcecegcgecedacbfgdagbgagafdegecadfebcdbgfacdecdegecadfebbcdfdeaabdbfgcbccfcaebcecfdfccagdafaeaacbggaefddebcbecdafageeaabcbdafadcbecdbcgcbdgedcfadbcaefbdfcbgfcdeceddaaffgcedfcdcgdcgbfdddfdadgagbbef...
output:
6625476
result:
wrong answer expected '5594', found '6625476'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%