贪心大法好...裸的贪心过掉后缀数组模板题,后缀数组代码待更...
#include<bits/stdc++.h>
using namespace std;
int n=0,tot=0,r[30010],l[30010],q[30010],tail2=0;
char s[30010];
void init()
{
char ch=getchar();
while(ch<‘0‘||ch>‘9‘)
{
ch=getchar();
}
while(ch<=‘9‘&&ch>=‘0‘)
{
n=n*10+ch-‘0‘;
ch=getchar();
}
while(tot<n)
{
ch=getchar();
if(ch<=‘Z‘&&ch>=‘A‘)
s[++tot]=ch;
}
}
bool pd(int head,int tail)
{
if(head>tail)return true;
while(head<=tail)
{
if(s[head]<s[tail])
{
return true;
}
else if(s[head]>s[tail])
{
return false;
}
else
{
if(r[head]==0&&l[tail]==0)
{
return pd(head+1,tail-1);
}
else if(r[head]!=0&&l[tail]!=0)
{
if(r[head]-head>tail-l[tail])
{
return false;
}
else if(r[head]-head<tail-l[tail])
{
return true;
}
else
{
if(s[r[head]]<s[l[tail]])
{
return true;
}
else if(s[r[head]]>s[l[tail]])
{
return false;
}
else
{
if(pd(r[head],l[tail]))
{
return true;
}
else
{
return false;
}
}
}
}
else if(r[head])
{
return true;
}
else
{
return false;
}
}
}
}
int main()
{
//freopen("bclgold.in","r",stdin);
//freopen("bclgold.out","w",stdout);
init();
int i,j,head=1,tail;
for(i=2;i<=n;i++)
{
if(s[i]!=s[i-1])
{
if(s[i]<s[i-1])
{
for(j=head;j<i;j++)
r[j]=i;
}
head=i;
}
}
head=n;
for(i=n-1;i>=1;i--)
{
if(s[i]!=s[i+1])
{
if(s[i]<s[i+1])
{
for(j=head;j>i;j--)
l[j]=i;
}
head=i;
}
}
head=1;tail=n;
while(head<=tail)
{
if(head)
if(tail==1518)
head=head;
if(s[head]<s[tail])
{
q[++tail2]=head;
head++;
}
else if(s[head]>s[tail])
{
q[++tail2]=tail;
tail--;
}
else
{
if(r[head]==0&&l[tail]==0)
{
q[++tail2]=head;
head++;
}
else if(r[head]!=0&&l[tail]!=0)
{
if(r[head]-head>tail-l[tail])
{
q[++tail2]=tail;
tail--;
}
else if(r[head]-head<tail-l[tail])
{
q[++tail2]=head;
head++;
}
else
{
if(s[r[head]]<s[l[tail]])
{
q[++tail2]=head;
head++;
}
else if(s[r[head]]>s[l[tail]])
{
q[++tail2]=tail;
tail--;
}
else
{
if(pd(r[head],l[tail]))
{
q[++tail2]=head;
head++;
}
else
{
q[++tail2]=tail;
tail--;
}
}
}
}
else if(r[head])
{
q[++tail2]=head;
head++;
}
else
{
q[++tail2]=tail;
tail--;
}
}
}
for(int i=1;i<=n;i++)
{
printf("%c",s[q[i]]);
if(i%80==0)
printf("\n");
}
if(n%80!=0)printf("\n");
return 0;
}