https://www.luogu.com.cn/problem/P2870
字典序最小显然贪心,若当前串首比串尾小,则取串首;若当前串首比串尾大,则取串尾。
那串首串尾一样呢?这个顺序显然会影响到后续操作。
考虑继续往内递归,如果碰到一样的,那么当前取什么都无所谓;若碰到不一样的,我们肯定是要取更小的那一边,因为这样才能更早的将那个更小的字符取出来。
这个过程实质是找字符串 \(s_{l,\cdots,r}\) 内第一个满足 \(s_{l+mid-1}\not=s_{r-mid+1}\) 的 \(mid\)。
方法一:二分+字符串哈希解决,时间复杂度 \(O(n\log n)\)。
方法二:发现这个过程的实质的实质是一个字符串和该字符串的反串比较大小,将原串反串之间隔一个奇怪字符做 SA,由于要求解的原串和反串一样则取那一边都无所谓,所以直接比较两串的 rk
即可。时间复杂度 \(O(n\log n)\)。
方法一 code:
点击查看代码
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
namespace Fread {
const int SIZE=1<<21;
char ibuf[SIZE],*S,*T;
inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
const int SIZE=1<<21;
char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
inline void putc(char c){*S++=c;if(S==T)flush();}
struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
if(x<0){x=-x;putchar('-');}
int y=0;char z[40];
while(x||!y){z[y++]=x%10+48;x/=10;}
while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=5e5+5,maxm=4e5+5,inf=0x3f3f3f3f;
const int mod[2]={998244353,1000000007},base=41;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m;
char s[maxn],ans[maxn];
int mi[maxn][2];
struct HashTable{
int f[maxn][2];
void init(){
rep(i,1,n)rep(j,0,1)f[i][j]=(f[i-1][j]*base%mod[j]+s[i]-'a')%mod[j];
}
int qry(int l,int r,int o){
return (f[r][o]-f[l-1][o]*mi[r-l+1][o]%mod[o]+mod[o])%mod[o];
}
}A,B;
void solve_the_problem(){
n=rd();
rep(i,1,n)cin>>s[i];
mi[0][0]=mi[0][1]=1;
rep(i,1,n)rep(j,0,1)mi[i][j]=mi[i-1][j]*base%mod[j];
A.init();
reverse(s+1,s+n+1);
B.init();
reverse(s+1,s+n+1);
int st=1,ed=n;
while(st<=ed){
if(s[st]<s[ed]){ans[++m]=s[st++];continue;}
if(s[st]>s[ed]){ans[++m]=s[ed--];continue;}
int len=(ed-st+1)/2;
int l=1,r=len,res=-1;
while(l<=r){
int mid=l+r>>1;
if(A.qry(st,st+mid-1,0)==B.qry(n-ed+1,n-ed+mid,0)&&A.qry(st,st+mid-1,1)==B.qry(n-ed+1,n-ed+mid,1)){
l=mid+1;
}else{
res=mid,r=mid-1;
}
}
if(res==-1||(s[st+res-1]<s[ed-res+1]))ans[++m]=s[st++];
else ans[++m]=s[ed--];
}
rep(i,1,n){pc(ans[i]);if(i%80==0)pc(10);}
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
// fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;while(_--)solve_the_problem();
}
/*
*/