本题同bzoj1640。。。双倍经验双倍幸福

 

 

虽然数据范围n=3w然而O(n²)毫无压力= =

http://blog.csdn.net/xueyifan1993/article/details/7773750

只要比较两个字符串的大小就行了= =果断hash?

具体一点的话就是从前往后和从后往前各hash一遍。。其他的懒得复述了= =

一开始开了unsigned long long然后被卡在了第二位。。。。。

然后作死用unsigned int直接68ms#1了2333

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ull unsigned int
using namespace std;
const int maxn=30003;
char s[maxn];
ull pre[maxn],jc[maxn],val1,val2,pre1[maxn];
short i,j,k,n,m,l,r,mid,len,L,R;

inline short getlen(){
    if(s[L]!=s[R])return 0;
    l=1;r=R-L+1;
    if(pre[L+r-1]-pre[L-1]*jc[r]==pre1[R-r+1]-pre1[R+1]*jc[r])return r;r--;
    while(l<r){
        mid=(l+r+1)>>1;//return mid;
        val1=pre[L+mid-1]-pre[L-1]*jc[mid];
        val2=pre1[R-mid+1]-pre1[R+1]*jc[mid];
        if(val1!=val2)r=mid-1;else l=mid;
        if(s[L+l]!=s[R-l])return l;
    }
    return l;
}
inline bool bigger(){
    if(s[L]!=s[R])return s[L]>s[R];
    len=getlen();
    if(len==R-L+1)return 1;
    else return s[L+len]>s[R-len];
}
int main(){
    scanf("%d",&n);
    for(i=1;i<=n;i++)for(s[i]=getchar();s[i]<'A'||s[i]>'Z';s[i]=getchar());
    jc[0]=1;for(i=1;i<=n;i++)jc[i]=jc[i-1]*107;
    for(i=1;i<=n;i++)pre[i]=pre[i-1]*107+(ull)s[i]-'A';
    for(i=n;i;i--)pre1[i]=pre1[i+1]*107+(ull)s[i]-'A';
    L=1;R=n;
    for(i=1;i<=n;i++)if(bigger()){
        putchar(s[R--]);
        if(i%80==0)putchar('\n');
    }else {putchar(s[L++]);if(i%80==0)putchar('\n');}
    return 0;
}
View Code

相关文章: