Why is my suffix array running so slow ...
Copy the string over to the last, then run SA and sweep it all over again ...
---------------------------------------------------------------------
#include <bits/stdc++.h>using namespace std;const int MAXN = 200009;struct HASH {int ID[MAXN], N;HASH () {N = 0;}inline void Add (int v) {id[n++] = v;}inline void work () {sort (ID, id + N);N = unique (ID, ID + N)-id;}inline int hash (int c) {return Lower_bound (ID, id + N, c)-id;}inline int _hash (int c) {return id[c];}} h;int SA[MAXN], RANK[MAXN], HEIGHT[MAXN], CNT[MAXN], N;Char S[MAXN];void Build_sa () {int m = H.N, *x = height, *y = rank;for (int i = 0; i < m; i++) cnt[i] = 0;for (int i = 0; i < N; i++) cnt[x[i] = s[i]]++;for (int i = 1; i < m; i++) cnt[i] + = cnt[i-1];for (int i = N-1; ~i; i--) sa[--cnt[x[i]] = i;for (int k = 1; k <= N; k <<= 1) {int p = 0;for (int i = n-k; i < N; i++) y[p++] = i;for (int i = 0; i < N; i++) if (Sa[i] >= k) y[p++] = sa[i]-K;for (int i = 0; i < m; i++) cnt[i] = 0;for (int i = 0; i < N; i++) cnt[x[y[i]]]++;for (int i = 1; i < N; i++) cnt[i] + = cnt[i-1];for (int i = N-1; ~i; i--) Sa[--cnt[x[y[i]]] [= y[i];swap (x, y); x[sa[0] = 0; p = 1;for (int i = 1; i < N; i++)X[sa[i]] = y[sa[i] [= y[sa[i-1]] && y[sa[i] + K] = = Y[sa[i-1] + K]? p-1: p++;if (P >= N) break;m = p;}}int main () {scanf ("%s", s);N = strlen (S);for (int i = 0; i < N; i++)H.add (s[i + N] = s[i]);N <<= 1;h.add (s[n++] =-1);h.work ();for (int i = 0; i < N; i++)S[i] = H.hash (S[i]);Build_sa ();for (int i = 0; i < N; i++) if (Sa[i] < N/2)Putchar (H._hash (S[sa[i] + N/2-1]));return 0;}
---------------------------------------------------------------------
1031: [JSOI2007] character encryption cipher time limit: ten Sec Memory Limit: 162 MB
Submit: 4199 Solved: 1704
[Submit] [Status] [Discuss] 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)