FZU 2267 贪心 后缀数组

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

还是太菜了。 给你两个数组 求合并的成一个数组 所能呈现的数的最大值,不能改变组内数组数字的顺序 贪心的策略就是 哪个选哪个,如果遇到两个相等,策略是 后面的哪个数大选哪个,但是复杂度达到了 o(n2) 超时,用后缀数组能快速求出 两者后面哪个大,需要先把两个串合并,得出 sa和ra 比较ra就好。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 2e5+100;
int t1[MAXN],t2[MAXN],c[MAXN];

bool cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}

void da(int str[],int sa[],int ra[],int height[],int n,int m)
{
    n++;
    int i,j,p,*x=t1,*y=t2;
    for(i=0;i<m;i++) c[i]=0;
    for(i=0;i<n;i++) c[x[i]=str[i]]++;
    for(i=1;i<m;i++) c[i]+=c[i-1];
    for(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=1;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]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        if(p>=n) break;
        m=p;
    }
    int k=0;
    n--;
    for(i=0;i<=n;i++) ra[sa[i]]=i;
    for(i=0;i<n;i++) 
    {
        if(k) k--;
        j=sa[ra[i]-1];
        while(str[i+k]==str[j+k])k++;
        height[ra[i]]=k;
    }
}

int ra[MAXN],height[MAXN];
int sa[MAXN],num[MAXN];
int str1[MAXN],str2[MAXN];
int ans[MAXN];

int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
            str1[i]=num[i];
        }
        num[n]=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d",&num[n+1+i]);
            str2[i]=num[n+i+1];
        }
        num[n+m+1]=0;
        da(num,sa,ra,height,n+m+1,120);
        int p1=0,p2=0;
        int tot=0;
        while(p1<n&&p2<m)
        {
            if(str1[p1]>str2[p2]) ans[tot++]=str1[p1++];
            else if(str1[p1]<str2[p2]) ans[tot++]=str2[p2++];
            else
            {
                if(ra[p1]>ra[n+1+p2])
                {
                    ans[tot++]=str1[p1++];
                }
                else{
                    ans[tot++]=str2[p2++];
                }
            }
        }
        while(p1<n) ans[tot++]=str1[p1++];
        while(p2<m) ans[tot++]=str2[p2++];
        printf("Case %d: ",cas );
        for(int i=0;i<tot;i++)
            printf("%d",ans[i] );
        printf("\n");
    }
}

转载于:https://www.cnblogs.com/dabai520/p/7554189.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值