http://www.lydsy.com/JudgeOnline/problem.php?id=1031

很容易想到这就是将字符串复制到自己末尾然后后缀数组搞出sa然后按区间输出即可。

然后换了下模板,将基数排序放到外边

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=200005;
char s[N];
int t1[N], t2[N], sa[N], c[N]; void st(int *x, int *y, int n, int m) {
int i;
for(i=0; i<m; ++i) c[i]=0;
for(i=0; i<n; ++i) c[ x[y[i]] ]++;
for(i=1; i<m; ++i) c[i]+=c[i-1];
for(i=n-1; i>=0; --i) sa[ --c[x[y[i]]] ]=y[i];
}
void hz(char *a, int n, int m) {
int i, j, p, *x=t1, *y=t2, *t;
for(i=0; i<n; ++i) x[i]=s[i], y[i]=i;
st(x, y, n, m);
for(j=1, p=1; p<n; j<<=1, m=p) {
for(p=0, i=n-j; i<n; ++i) y[p++]=i;
for(i=0; i<n; ++i) if(sa[i]>=j) y[p++]=sa[i]-j;
st(x, y, n, m);
for(t=x, x=y, y=t, p=1, x[sa[0]]=0, i=1; i<n; ++i)
x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
}
} int main() {
scanf("%s", s+1);
int n=strlen(s+1);
for1(i, 1, n) s[i+n]=s[i];
s[0]=0;
hz(s, n*2+1, 256);
int tot=1, i=1;
while(tot<=n) {
//dbg(sa[i]);
if(sa[i]<=n) {
printf("%c", s[sa[i]+n-1]);
++tot;
}
++i;
}
return 0;
}

原版的后缀数组:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=200005;
char s[N];
int t1[N], t2[N], sa[N], c[N]; void hz(char *a, int n, int m) {
int i, j, p, *x=t1, *y=t2, *t;
for(i=0; i<m; ++i) c[i]=0;
for(i=0; i<n; ++i) c[ x[i]=a[i] ]++;
for(i=1; i<m; ++i) c[i]+=c[i-1];
for(i=n-1; i>=0; --i) sa[ --c[x[i]] ]=i;
for(j=1, p=1; p<n; j<<=1, m=p) {
for(p=0, i=n-j; i<n; ++i) y[p++]=i;
for(i=0; i<n; ++i) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0; i<m; ++i) c[i]=0;
for(i=0; i<n; ++i) c[ x[y[i]] ]++;
for(i=1; i<m; ++i) c[i]+=c[i-1];
for(i=n-1; i>=0; --i) sa[ --c[x[y[i]]] ]=y[i];
for(t=x, x=y, y=t, p=1, x[sa[0]]=0, i=1; i<n; ++i)
x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
}
} int main() {
scanf("%s", s+1);
int n=strlen(s+1);
for1(i, 1, n) s[i+n]=s[i];
s[0]=0;
hz(s, n*2+1, 256);
int tot=1, i=1;
while(tot<=n) {
//dbg(sa[i]);
if(sa[i]<=n) {
printf("%c", s[sa[i]+n-1]);
++tot;
}
++i;
}
return 0;
}

  


Description

喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考。一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法。例如下图,可以读作:

JSOI07 SOI07J OI07JS I07JSO 07JSOI 7JSOI0 把它们按照字符串的大小排序: 07JSOI 7JSOI0 I07JSO JSOI07 OI07JS SOI07J 读出最后一列字符:I0O7SJ,就是加密后的字符串(其实这个加密手段实在很容易破解,鉴于这是突然想出来的,那就^^)。但是,如果想加密的字符串实在太长,你能写一个程序完成这个任务吗?

Input

输入文件包含一行,欲加密的字符串。注意字符串的内容不一定是字母、数字,也可以是符号等。

Output

输出一行,为加密后的字符串。

Sample Input

JSOI07

Sample Output

I0O7SJ

HINT

对于100%的数据字符串的长度不超过100000。

Source

【BZOJ】1031: [JSOI2007]字符加密Cipher(后缀数组)的更多相关文章

  1. BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题

    BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题 将字符串复制一遍接在原串后面,然后后缀排序即可. #include <cmath> #include &l ...

  2. BZOJ 1031 [JSOI2007]字符加密Cipher 后缀数组教程

    1031: [JSOI2007]字符加密Cipher Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一 ...

  3. BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6014  Solved: 2503[Submit ...

  4. bzoj 1031 [JSOI2007]字符加密Cipher 后缀数组模板

    题目链接 题目描述 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作: ...

  5. bzoj 1031: [JSOI2007]字符加密Cipher 後綴數組模板題

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3157  Solved: 1233[Submit ...

  6. 【BZOJ1031】[JSOI2007]字符加密Cipher 后缀数组

    [BZOJ1031][JSOI2007]字符加密Cipher Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的 ...

  7. 【BZOJ 1031】[JSOI2007]字符加密Cipher(后缀数组模板)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1031 [题意] [题解] 后缀数组模板题; 把整个字符串扩大一倍. 即长度乘2 然后搞 ...

  8. 1031. [JSOI2007]字符加密【后缀数组】

    Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作 ...

  9. bzoj 1031 [JSOI2007]字符加密Cipher

    求出来后缀数组的rank就行了,不会可以去看集训队论文. #include<iostream> #include<cstdio> #include<cstring> ...

  10. bzoj 1031: [JSOI2007]字符加密Cipher【后缀数组】

    算是SA的裸题了 把串复制一遍接在原串后面,然后求SA,然后按着SA的顺序输出尾字符即可 #include<iostream> #include<cstdio> #includ ...

随机推荐

  1. java泛型通配符?

    转自:http://www.linuxidc.com/Linux/2013-10/90928.htm T  有类型 ?  未知类型 一.通配符的上界 既然知道List<Cat>并不是Lis ...

  2. ant design pro (十三)advanced 错误处理

    一.概述 原文地址:https://pro.ant.design/docs/error-cn 二.详细 2.1.页面级报错 2.1.1.应用场景 路由直接引导到报错页面,比如你输入的网址没有匹配到任何 ...

  3. UG NX9.0.0 for linux安装

    自:http://vivianyw.blog.163.com/blog/static/13454742220145601415881/?latestBlog NX9.0.0 for linux安装. ...

  4. linux下php添加cur/soapl扩展

    注意:在不同的扩展路径下 ./configure --help 的帮助信息不尽相同 1.跟php一起安装 下载 http://curl.haxx.se/download/ curl 取较低的版本 wg ...

  5. Intellij IDEA 14使用maven3.3.3 问题

    Intellij IDEA 14使用maven3.3.3报错: -Dmaven.multiModuleProjectDirectory system propery is not set. Check ...

  6. 【LeetCode】100. Same Tree (2 solutions)

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  7. mysql优化一

    1.show global status      可以列出MySQL服务器运行各种状态值 2.show variables            查询MySQL服务器配置信息 一.慢查询 mysql ...

  8. Silver Cow Party

    Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...

  9. JSON对象和JSON字符串以及JSON.parse 函数的使用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. [转]Oracle 10g如何对用户姓名,按首字母排序、查询

    首先介绍Oracle 9i新增加的一个系统自带的排序函数  1.按首字母排序  在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值      SCHINESE_RADICA ...