FZU 2267 贪心 后缀数组
来源:互联网 发布:有域名了怎么做网站 编辑:程序博客网 时间:2024/06/16 20:37
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"); }}
- FZU 2267 贪心 后缀数组
- 第七届福建省赛 FZU 2267 The Bigger the Better(贪心 后缀数组)
- FZU Problem 2267 The Bigger the Better(后缀数组)
- 后缀数组+贪心+poj3623
- fzu 2137 字符串hash或者后缀数组
- 【FZU】Problem 2137 奇异字符串【后缀数组】
- FZU 2137 奇异字符串 后缀数组
- poj 3623 贪心,后缀数组
- 后缀数组+贪心+隔板法
- BZOJ 1692 队列变换 贪心+后缀数组
- [后缀数组+贪心] poj 3518 Sequence
- BZOJ 4278: [ONTAK2015]Tasowanie|后缀数组|贪心
- 4278: [ONTAK2015]Tasowanie 后缀数组+贪心
- 【bzoj1692】【队列变幻】【贪心+后缀数组】
- [BZOJ4278][ONTAK2015]Tasowanie(后缀数组+贪心)
- [后缀数组 贪心] BZOJ 4278 [ONTAK2015]Tasowanie
- [贪心+后缀数组] poj 3623 Best Cow Line, Gold
- 1692: [Usaco2007 Dec]队列变换 后缀数组+贪心
- Number Steps
- C++11 lambda 表达式解析
- ==和=的区别
- 安卓教程:Xposed 框架安装及使用
- Mybatis(三)— 关联查询
- FZU 2267 贪心 后缀数组
- 一维转二维数组
- Springboot Redis分布式集群(1)- Linux下redis安装与使用
- 网络基础 — OSI七层协议模型、TCP/IP四层模型
- POJ1066---Treasure Hunt (计算几何基础:线段相交)
- [未完待续][NOI2017模拟]大新闻
- Count Color POJ 2777
- MRP运行(MD01-MD03) MRP控制参数
- springboot集成security