http://www.lydsy.com/JudgeOnline/problem.php?id=1031
It is easy to think of this as copying the string to its end and then the suffix array out of the SA and output by the interval.
Then change the template and put the cardinal sort outside
#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, in T> #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); Define CC (I,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) #define DBG (x) cout <& Lt (#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 << endlinline 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,); int tot=1, I=1;while (to T<=n) {//dbg (Sa[i]), if (sa[i]<=n) {printf ("%c", s[sa[i]+n-1]); ++tot;} ++i;} return 0;}
Suffix array of the original:
#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, in T> #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); Define CC (I,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) #define DBG (x) cout <& Lt (#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 << endlinline 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;f or (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,); int tot=1, I=1;while (to T<=n) {//dbg (Sa[i]), if (sa[i]<=n) {printf ("%c", s[sa[i]+n-1]); ++tot;} ++i;} return 0;}
Description
Like to delve into the problem of JS classmate, and recently fascinated by the encryption method of thinking. One day, he suddenly came up with what he thought was the ultimate encryption: to make a circle of information that needs to be encrypted, it is clear that they have many different ways of reading. For example, it can be read as:
JSOI07 soi07j oi07js i07jso 07JSOI 7jsoi0 sort them by the size of the string: 07JSOI 7jsoi0 i07jso JSOI07 oi07js soi07j read the last column of characters: I0O7SJ, the word after the encryption String (in fact, this encryption method is very easy to crack, because it is suddenly thought out, then ^ ^). However, if the string you want to encrypt is too long, can you write a program to accomplish this task?
Input
The input file contains a row of strings to encrypt. Note that the contents of a string are not necessarily letters, numbers, or symbols.
Output
The output line is the encrypted string.
Sample Inputjsoi07sample Outputi0o7sjhint
The length of the data string for 100% does not exceed 100000.
Source
"Bzoj" 1031: [JSOI2007] character encryption cipher (suffix array)