1031: [JSOI2007] character encryption cipher
Time limit:20 Sec Memory limit:256 MB
Topic Connection http://acm.hust.edu.cn/problem/show/1590
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?
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
Test Instructions
Exercises
Suffix array naked, first extend the string by half, then run a suffix array, he can sort himself
Then judge the length of the string, and then the output will be fine.
The suffix array code is shown below, and has been annotated.
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 1000001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;} while (x>0) ch[++num]=x%10,x/=10; while (Num) Putchar (ch[num--]+48); Puts ("");}*/inline ll read () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;} while(x>0) ch[++num]=x%Ten, x/=Ten; while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************Chars[2*MAXN];intN;intSA[MAXN],RA[MAXN],TMP[MAXN],HEIGHT[MAXN];inttop[maxn+Ten];voidBuild_sa () {intm = -;//Maximum character value int*x = ra, *y = tmp;//x First keyword y backup//to sort a single character in cardinal order for(inti =0; I < m; i++) Top[i] =0; for(inti =0; I < n; i++) Top[x[i] = s[i]]++; for(inti =1; I < m; i++) Top[i] + = top[i-1]; for(inti = n1; I >=0; i--) Sa[--top[x[i]] =i; for(intK =1; K <= N; K <<=1) { intp =0; //use the last SA value to calculate the ordering of the second keyword for(inti = n-k; I < n; i++) y[p++] =i; for(inti =0; I < n; i++)if(Sa[i] >= k) y[p++] = Sa[i]-K; //sort the first keyword in cardinal order for(inti =0; I < m; i++) Top[i] =0; for(inti =0; I < n; i++) top[x[y[i]]]++; for(inti =0; I < m; i++) Top[i] + = top[i-1]; for(inti = n1; I >=0; i--) sa[--top[x[y[i] []] =Y[i]; Swap (x, y); P=1; x[sa[0]] =0; //Calculate Rank Value for(inti =1; I < n; i++) X[sa[i]]= (y[sa[i-1] [= Y[sa[i]] && y[sa[i-1]+k] = = Y[sa[i]+k])? P1: p++; if(P >= N) Break; M=p; }}voidbuild_height () {//Rep (i,0,n) rank[sa[i]] = i; intK =0; for(intI=0; i<n;i++) Ra[sa[i]] =i; for(intI=0; i<n;i++){ if(k) k--; intj = Sa[ra[i]-1]; while(S[i+k] = = S[j+k]) k++; Height[ra[i]]=K; }}intMain () {scanf ("%s", s); intLen =strlen (s); for(inti=len;i<2*len;i++) S[i] = s[i-Len]; N=2*Len; Build_sa (); for(intI=1; i<n;i++) { if(Sa[i] <Len) {Putchar (S[sa[i]+len-1]); }} puts ("");}
Bzoj 1031: [JSOI2007] character encryption cipher suffix array