首页 > Web开发 > 详细

Luogu P4051 [JSOI2007]字符加密

时间:2020-01-10 20:10:23      阅读:109      评论:0      收藏:0      [点我收藏+]

题意:给出一个字符串 \(S\) ,将 \(S\) 所有的循环同构串按字典序排序,按顺序输出排序后每个串的首字符。

\(SA\) 把串复制一倍拼在后面,然后进行后缀排序即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#define R register int
using namespace std;
namespace Luitaryi {
inline int g() { R x=0,f=1;
  register char s; while(!isdigit(s=getchar())) f=s=='-'?-1:f;
  do x=x*10+(s^48); while(isdigit(s=getchar())); return x*f;
} const int N=200010;
int n,l,sa[N],x[N],y[N],c[N];
char s[N];
inline void pre() {
  R m=122;
  for(R i=1;i<=n;++i) ++c[x[i]=s[i]];
  for(R i=1;i<=m;++i) c[i]+=c[i-1];
  for(R i=n;i;--i) sa[c[x[i]]--]=i;
  for(R t=1,top=0;top<n;m=top,t<<=1) {
    top=0;
    for(R i=n-t+1;i<=n;++i) y[++top]=i;
    for(R i=1;i<=n;++i) if(sa[i]>t) y[++top]=sa[i]-t;
    memset(c,0,(m+1)<<2);
    for(R i=1;i<=n;++i) ++c[x[i]];
    for(R i=1;i<=m;++i) c[i]+=c[i-1];
    for(R i=n;i;--i) sa[c[x[y[i]]]--]=y[i];
    swap(x,y),x[sa[1]]=top=1;
    for(R i=2;i<=n;++i)
      x[sa[i]]=(y[sa[i]]==y[sa[i-1]] && y[sa[i]+t]==y[sa[i-1]+t])?top:++top;
  }
}
inline void main() {
  scanf("%s",s+1),n=strlen(s+1);
  memmove(s+n+1,s+1,n),l=n,n<<=1;
  pre(); for(R i=1;i<=n;++i) 
    if(sa[i]<=l) putchar(s[sa[i]+l-1]);
} 
} signed main() {Luitaryi::main(); return 0;}

\(SAM\) ...没必要吧。


2020.01.09

Luogu P4051 [JSOI2007]字符加密

原文:https://www.cnblogs.com/Jackpei/p/12177294.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!