FZU Problem 2267 The Bigger the Better(后缀数组)

来源:互联网 发布:投影软件 编辑:程序博客网 时间:2024/06/06 04:10

Problem 2267 The Bigger the Better

Accept: 81    Submit: 456
Time Limit: 1500 mSec    Memory Limit : 32768 KB

 Problem Description

Fat brother and Maze are playing a kind of special (hentai) game with two integers. All the digits of these two integers are in the range from 1 to 9. After they’ve got these two integers, they thought that these two numbers were not large enough! (You know that the young people always like the HUGE THING in order to have more pleasure) So they decide to use the digits of these two integers to make a new BIGGER integer. At the beginning of this game, the new integer has no digit, each time Fat Brother and Maze can choose one of the two initial integers (if this integer exists) and move its first digit to the end of the new integer. For instance, if the new integer is 233 and the two initial integers are 3154 and 1324 now, they can choose the first digit of the integer 3154, which is 3, and add it to the end of the new integer to make it become 2333. The two initial integers are 154 and 1324 now after this action. Also they can choose the first digit of the integer 1324 and add it to the end of the integer 233 and make it become 2331. This process goes until the two initial integers are all empty. Now Fat Brother and Maze would like to know the maximum number they could get after this special (hentai) game.

 Input

The first line of the date is an integer T (1 <= T <= 102), which is the number of the text cases.

Then T cases follow, each case contains two integers N and M (1 <= N,M <= 100000) indicated the number of the digits of these two integers. Then a line with N digits indicates the first integer and a line with M digits indicates the second integer. Note that all the digits are in the range from 1 to 9.

In 90% of test cases, N, M <= 1000.

 Output

For each case, output the case number first, and then output the integer Fat Brother and Maze would get, this integer should be as large as possible.

 Sample Input

1
3 4
2 5 2
3 6 3 1

 Sample Output

Case 1: 3632521

题意:给两个数组每次可以从一个数组的开头选一个数放到新数组的末尾,求构成的最大的新数组

解;当两个数不一样大时直接选取结果,当一样大时用后缀数组的RANK 数组比较大小

注(因为 n没加1 wa 出翔)。。。

void build_sa(int s[],int n,int m))

#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;const int N = 300000+100;int sa[N];int t1[N],t2[N],c[N];int rank1[N],height[N];void build_sa(int s[],int n,int m){    int i,j,p,*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=0; i<m; i++) c[i]+=c[i-1];    for(int i=n-1; i>=0; i--) sa[--c[x[i]]]=i;    for(j=1; j<=n; j<<=1)    {        p=0;        for(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=0; i<m; i++) c[i]+=c[i-1];        for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;        x[sa[0]]=0;        for(i=1; i<n; i++)            x[sa[i]]=(y[sa[i-1]]==y[sa[i]])&&(y[sa[i-1]+j]==y[sa[i]+j])?p-1:p++;        if(p>=n) break;        m=p;    }}void getheight(int s[],int n){    int i, j, k=0;    for(i=0;i<=n;i++) rank1[sa[i]]=i;    for(i=0;i<n;i++)    {        if(k) k--;        j=sa[rank1[i]-1];        while(s[i+k]==s[j+k]) k++;        height[rank1[i]]=k;    }    return ;}int s[N];int ans[N];int main(){    int t, ncase=1;    scanf("%d", &t);    while(t--)    {        int n, m, i, j, k=0, x, cnt=0;        scanf("%d %d", &n, &m);        for(int i=0;i<n;i++) scanf("%d", &x),s[k++]=x;        s[k++]=0;        for(int i=0;i<m;i++) scanf("%d", &x),s[k++]=x;        s[k]=0;        build_sa(s,k+1,127);//k要加1        getheight(s,k);        i=0, j=n+1;        while(i<n&&j<n+1+m)        {            if(s[i]<s[j]) ans[cnt++]=s[j],j++;            else if(s[i]>s[j]) ans[cnt++]=s[i],i++;            else if(rank1[i]<rank1[j]) ans[cnt++]=s[j],j++;            else ans[cnt++]=s[i],i++;        }        while(i<n) ans[cnt++]=s[i],i++;        while(j<n+1+m) ans[cnt++]=s[j],j++;        printf("Case %d: ",ncase++);        for(int i=0;i<cnt;i++) printf("%d",ans[i]);        printf("\n");    }    return 0;}