bzoj1692 queue changing greedy + suffix array __ suffix array

Source: Internet
Author: User
Description

FJ is going to take his n (1 <= n <= 30,000) cow to the annual "American Farmers ' Grand Prix". In this game, each contestant must put his cows in a row, and then lead them to pass in front of the referee. This year, the competition Committee adopted a new registration rule when it accepted the queue: they took the first letter of the cows ' names from all the teams and ranked them in the order of the cows in the ranks (for example, if the FJ brought the cows to Bessie, Sylvia, Dora, The registration officer registers the team as BSD. After the registration, the organizing committee will arrange the registration names of all the teams in ascending order of dictionary order, and get their appearance sequence. FJ has had a lot of things to do recently, so he's not going to waste too much time on the game, which means he wants to play as early as possible. So he was going to readjust the team type that the cows had planned. FJ's adjustment approach is that each time he pulls a cow at the top or end of the original queue, arranges her to the tail of the new queue, and repeats the operation on the remaining cows ' queues until all the cows are plugged into the new queue. This gets the queue, which is FJ to register the final cow queue. The next thing is for you: for a given cow's initial position, calculate the queue with the smallest dictionary order possible according to the FJ Adjustment rules. Input

* Line 1th: An integer: N

* 2nd.. N+1 Line: i+1 only 1 ' a '. Z ' letter, indicating the first letter of the name of the cow from the end of the queue

* 1th ...?? Row: The queue with the smallest dictionary order output FJ can get. Each row (except the last line) outputs exactly 80 ' a '. Z ' letter, representing the first letter of each cow's name in the new queue Sample Input 6
A
C
D
B
C
B

Enter description:

FJ has 6 headers in sequence The cows lined up: ACDBCB

Sample output ABCBCD

Output Description:

Operand original queue new queue
#1 ACDBCB
#2 CDBCB A
#3 cdbc AB
#4 CDB ABC
#5 CD ABCB
#6 D abcbc
#7 ABCBCD
: First, if the left and right words are not the same, it must be a small selection of dictionaries. If the same words can be compared to the left character The suffix at the beginning and the prefix that begins with the right character.   So the template string is reversed to the back and a separator is added in the middle. Sets the suffix array. The prefix can then be converted to a suffix for direct comparison. Code:

#include <iostream> #include <cstdio> #include <cstring> #define N 60010 using namespace std;
int s[n],c[n],t1[n],t2[n],sa[n],rank[n],n,m,temp;
Char ch;
  BOOL CMP (int *y,int p,int q,int k) {int a= (p+k>=n) -1:y[p+k];
  int b= (q+k>=n)? -1:y[q+k];
Return a==b&&y[p]==y[q];
  } void Build_sa (int m) {int *x=t1,*y=t2;
  for (int i=0;i<m;i++) c[i]=0;
  for (int i=0;i<n;i++) c[x[i]=s[i]]++;
  for (int i=1;i<m;i++) c[i]+=c[i-1];
  for (int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
    for (int k=1;k<=n;k<<=1) {int = =;
    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++) c[i]=0;
    for (int i=0;i<n;i++) c[x[y[i]]]++;
    for (int i=1;i<m;i++) c[i]+=c[i-1];
    for (int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
    Swap (x,y);
    p=1;x[sa[0]]=0;
    for (int i=1;i<n;i++) x[sa[i]]=cmp (y,sa[i],sa[i-1],k)? p-1:p++;
    if (p>=n) break; 
  M=p;
 int main () { scanf ("%d", &n); s[n]= ' $ ';
   for (int i=0;i<n;i++) {scanf ("%*c%c", &ch);
  S[2*n-i]=s[i]=ch; 
  } m=n;n=n+n+1;build_sa (2666);
  for (int i=0;i<n;i++) rank[sa[i]]=i;
  for (int i=0;i<n;i++) cout<<sa[i]<< ';
  int l=0,r=m-1;
    while (l<r) {int a=rank[l],b=rank[m+m-r];
    cout<<l<< ' <<r<< ' <<a<< ' <<b<< ';
    if (a<b) {printf ("%c", S[l]); l++;
    else{printf ("%c", S[r]); r--;
    } temp++;
  if (temp>=80&& (l!=r)) {puts (""); temp=0;}}
printf ("%c", S[l]);  }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.