Sequence POJ - 3581 suffix array

Meaning of the questions:

A non-empty sequence into three parts, each part will be turned over after combined into a new sequence,

Output sequence thus obtained lexicographically smallest sequence 

(Ensure that the first number is the largest element in the array)

 

answer:

The array as a string.

First because the maximum number, so we can turn first string, then the first can find a sa [i]> 1,

Because sa [i] is the lexicographic order of ascending order.

Then the second part of the process for me is to look at solutions to problems.

Thus the second portion can not be directly solved

E.g:

After removal of the first portion of the sequence is 4322, if the lexicographically smallest directly select string is 2, then the final solution is 2432 clearly wrong

But we will become double after the string 43224322 4 before lexicographically smallest number is to 224,322, and then out of the double portion 4322 

This is optimal for solving the

The remaining direct supplement can be.

 

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <set>
  7 #include <iostream>
  8 #include <map>
  9 #include <stack>
 10 #include <string>
 11 #include <time.h>
 12 #include <vector>
 13 #define  pi acos(-1.0)
 14 #define  eps 1e-9
 15 #define  fi first
 16 #define  se second
 17 #define  rtl   rt<<1
 18 #define  rtr   rt<<1|1
 19 #define  bug         printf("******\n")
 20 #define  mem(a,b)    memset(a,b,sizeof(a))
 21 #define  name2str(x) #x
 22 #define  fuck(x)     cout<<#x" = "<<x<<endl
 23 #define  f(a)        a*a
 24 #define  sf(n)       scanf("%d", &n)
 25 #define  sff(a,b)    scanf("%d %d", &a, &b)
 26 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
 27 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
 28 #define  pf          printf
 29 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
 30 #define  FREE(i,a,b) for(i = a; i >= b; i--)
 31 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
 32 #define  FRLL(i,a,b) for(i = a; i > b; i--)
 33 #define  FIN         freopen("data.txt","r",stdin)
 34 #define  gcd(a,b)    __gcd(a,b)
 35 #define  lowbit(x)   x&-x
 36 #define rep(i,a,b) for(int i=a;i<b;++i)
 37 #define per(i,a,b) for(int i=a-1;i>=b;--i)
 38 
 39 using namespace std;
 40 typedef long long  LL;
 41 typedef unsigned long long ULL;
 42 const int maxn = 1e6 + 7;
 43 const int maxm = 8e6 + 10;
 44 const int INF = 0x3f3f3f3f;
 45 const int mod = 10007;
 46 
 47 //rnk从0开始
 48 //sa starting from 1, because the last character (minimum) at No. 0
 49  // height from the beginning, as represented by the sa [i - 1] and sa [I]
 50  // multiplication algorithm is O (nlogn) 
51 is  int WA [MAXN], WB [MAXN], WV [MAXN], WS_ [MAXN];
 52 is  int Rank [MAXN], height [MAXN], SA [MAXN], R & lt [MAXN];
 53 is  int n-, Maxx;
 54 is  char S [MAXN];
 55  // range function parameter m represents Suffix characters in the string, radix sorting is a parameter, if the original is a letter can be taken directly sequence 128, if the original sequence itself are integers , then m may take a value larger than the maximum integer of 1
 56  // be sorted string in array r from r [0] to r [n-1], a length of n-
 57 is  // for comparison size, may be added after a character string, the character does not appear in the preceding character, and the character to be smaller than the previous
 58  // supra, in order to facilitate the operation function, convention r [n-1] in addition to the outer All r [i] is greater than 0, r [n-1]
 = 059  // After the function, the result in array sa, sa from [0] to sa [. 1-n-] 
60  void Suffix ( int * R & lt, int * sa, int n-, int m) {
 61 is      int I, J , K, WA = X *, Y * = WB, * T;
 62 is      // length of string collation 1
 63      // in general, the title character string, the maximum value of r will not be great, so used herein, the radix sort
 64      // If the maximum value of r is large, then this code into quicksort 
65      for (I = 0 ; I <m; I ++) WS_ [I] = 0 ;
 66      for (I = 0 ; I <n-; I ++) WS_ [X [I] = R & lt [I]] ++; // count the number of characters 
67     for (i = . 1 ; i <m; i ++) WS_ [i] + = WS_ [i - . 1 ]; // count i is not greater than the number of characters in the character 
68      for (i = n-- . 1 ; i> = 0 ; Inc. (www.i-levelmedia.com)) SA [- WS_ [x [I]]] = I; // calculate rank character
 69      // radix sort
 70      // value x stored in the array corresponds to a rank value 
71 is      for (J = . 1 , = K . 1 ; K <n-; J * = 2 , m = K) {
 72          // J is the current length of the string, the result is stored in an array of y of the second sort key
 73          // second keyword ranking 
74          for (K = 0, = N-I - J; I <n-; I ++) Y [K ++] = I; // second keyword is 0 top surface 
75          for (I = 0 ; I <n-; I ++) IF (sa [i]> = j) Y [K ++] = sa [i] - j; // length j of the substring of sa [i] should be of length 2 * j of the substring of sa [i] - j of suffix (second key) as a 2 * j substrings of all sorted according to the length of a second key 
76          for (I = 0 ; I <n-; I ++) WV [I] = X [Y [ I]]; // extracting a first key
 77          // Sort the first key (on the same principle as the length of the string collation 1) 
78          for (I = 0 ; I <m; I ++) WS_ [I ] = 0 ;
 79          for (I = 0 ; I <n-; I ++) WS_ [WV [I]] ++ ;
 80          for(I = . 1 ; I <m; I ++) WS_ [I] + = WS_ [I - . 1 ];
 81          for (n-I = - . 1 ; I> = 0 ; Inc. (www.i-levelmedia.com)) SA [- WS_ [ WV [I]]] = Y [I]; // press a first key, to calculate the length of 2 * j substrings ranking
 82          // in this case the array x of length ranking of the j substrings , y array is still based on a result of the second sort key
 83          // calculate the length of ranking 2 * j of the substring, saved to the array X 
84          T = X;
 85          X = y;
 86          y = T;
 87          for (X [SA [ 0 ]] = 0 , I = K = . 1 ; I <n-; ++ I)
 88             X [SA [I]] = (Y [SA [I - . 1 ]] == Y [SA [I]] && Y [SA [I - . 1 ] + J] == Y [SA [I] + J] ?) K - . 1 : K ++ ;
 89          // If the length of 2 * j of the substring of sa [i] with the sa [i - 1] are identical, they have the same rank 
90      }
 91 is  }
 92  void calheight ( int * R & lt, int * SA, int n-) {
 93      int I, J, K = 0 ;
 94      for (I = . 1 ; I <= n-; I ++) Rank [SA [I]] = I;
 95      for (I = 0 ; I <n-; height [Rank [I ++]] = K)
 96         for ( k ? k-- : 0, j = sa[Rank[i] - 1]; r[i + k] == r[j + k]; k++ );
 97 }
 98 
 99 int num, b[maxn], ans[maxn];
100 int main() {
101     sf ( num );
102     n = num;
103     for ( int i = 0 ; i < num ; i++ ) {
104         scanf ( "%d", &r[num - i - 1] );
105         b[num - i] = r[num - i - 1];
106     }
107     sort ( b + 1, b + 1 + num );
108     int len = unique ( b + 1, b + 1 + num ) - b - 1;
109 //    for ( int i = 1 ; i <= len ; i++ ) printf ( "%d%c", b[i], ( i == len ? '\n' : ' ' ) );
110     for ( int i = 0 ; i < num ; i++ ) r[i] = lower_bound ( b + 1, b + 1 + len, r[i] ) - b, maxx = max ( maxx, r[i] );
111     r[n] = 0;
112 //    for ( int i = 0 ; i <= n ; i++ ) printf ( "%d%c", r[i], ( i == n ? '\n' : ' ' ) );
113     Suffix ( r, sa, n + 1, maxx + 1 );
114     calheight ( r, sa, n );
115     int idx = 0;
116     for ( int i = 1 ; i <= n ; i++ ) {
117 //        printf ( "%d%c", sa[i], ( i == n ? '\n' : ' ' ) );
118         if ( sa[i] > 1 ) {
119             idx = sa[i];
120             break;
121         }
122     }
123     len = 0;
124     for ( int i = idx ; i < num ; i++ ) ans[++len] = r[i];
125 //    for ( int i = 1 ; i <= len ; i++ ) printf ( "%d%c", b[ans[i]], ( i == len ? '\n' : ' ' ) );
126     n = 2 * idx, maxx = 0;
127     for ( int i = idx ; i < 2 * idx ; i++ ) r[i] = r[i - idx], maxx = max ( maxx, r[i] );
128     r[n] = 0;
129 //    for (int i=0 ;i<n ;i++) printf("%d ",r[i]);
130 //    printf("\n");
131     Suffix ( r, sa, n + 1, maxx + 1 );
132     calheight ( r, sa, n );
133     for ( int i = 1 ; i <= n ; i++ ) {
134         if ( sa[i] > 0 && sa[i] < idx ) {
135             idx = sa[i];
136             break;
137         }
138     }
139 //    fuck(idx);
140     for ( int i = idx ; i < n / 2 ; i++ ) ans[++len] = r[i];
141 
142     for ( int i = 0 ; i < idx ; i++ ) ans[++len] = r[i];
143     for ( int i = 1 ; i <= len ; i++ ) printf ( "%d\n", b[ans[i]] );
144 
145     return 0;
146 }
View Code

 

Guess you like

Origin www.cnblogs.com/qldabiaoge/p/11330247.html