POJ 题目3623 Best Cow Line, Gold(后缀数组rank简单应用)
来源:互联网 发布:白象梳篦淘宝店铺 编辑:程序博客网 时间:2024/05/17 12:56
Description
FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.
The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows' names.
FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.
FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.
Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in theith position in the original line
Output
The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.
Sample Input
6ACDBCB
Sample Output
ABCBCD
Source
USACO 2007 December Gold
题目大意:只能从两头取,使字典序最小
ac代码
Problem: 3623 User: kxh1995 Memory: 1364K Time: 438MS Language: C++ Result: Accepted
#include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> #include<iostream> #define min(a,b) (a>b?b:a) #define max(a,b) (a>b?a:b) #define INF 0xfffffff #define N 60060 using namespace std; char str[N];int sa[N],t1[N],t2[N],c[N];int rank[N];void build_sa(char *s,int n,int m){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]=s[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);int *T=x;x=y;y=T;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;}}int main(){int n;while(scanf("%d",&n)!=EOF){char s[2];int i;for(i=0;i<n;i++){scanf("%s",s);str[i]=s[0];str[n*2-i]=s[0];}str[n]=0;str[2*n+1]=0;build_sa(str,2*n+2,200);for(i=0;i<2*n+2;i++)rank[sa[i]]=i;int l=0,r=n+1;int len=0;while((len++)<n){if(rank[l]<rank[r]){printf("%c",str[l]);l++;}else{printf("%c",str[r]);r++;}//len++;if(len%80==0)printf("\n");}if(len%80)printf("\n");}}
- POJ 题目3623 Best Cow Line, Gold(后缀数组rank简单应用)
- [贪心+后缀数组] poj 3623 Best Cow Line, Gold
- |Poj 3623|后缀数组|Best Cow Line, Gold
- POJ3623:Best Cow Line, Gold(后缀数组)
- POJ3623 Best Cow Line, Gold 【后缀数组】
- POJ-3623-Best Cow Line, Gold
- poj 3623 Best Cow Line, Gold
- POJ 3623 Best Cow Line, Gold(模拟)
- POJ 3623 Best Cow Line, Gold(模拟)
- poj 3623 Best Cow Line, Gold(贪心)
- POJ 3623Best Cow Line, Gold
- POJ 3623 Best Cow Line, Gold(贪心)
- POJ 3623 Best Cow Line, Gold(贪心)
- poj 3623 Best Cow Line, Gold 暴力枚举
- 后缀数组——洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
- PKU 3623 Best Cow Line, Gold
- poj3623 Best Cow Line, Gold(贪心)
- POJ3623 Best Cow Line,Gold
- 自定义数据库同步程序
- Jpa规范中persistence.xml 配置文件解析
- USB Type-C
- SDUT-3303-来发背包开开胃
- 48.iOS动画和理解position与anchorPoint
- POJ 题目3623 Best Cow Line, Gold(后缀数组rank简单应用)
- uvalive5983(二分+dp)
- C++11特有的数值、数组初始化方法、常量的符号名称 const和浮点数、bool、自动推断类型auto
- Linux xargs 命令
- Mysql导出表结构及表数据 mysqldump用法
- 3.solr5全量索引和增量索引配置
- 【笔试】31、在O(1)时间删除链表节点
- django 1.8 官方文档翻译: 1-1-1 Django初探
- Windows上用VAGRANT创建运行管理VirtualBox虚拟机