Best Cow Line, Gold
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6038   Accepted: 2087

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 the ith 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

6
A
C
D
B
C
B

Sample Output

ABCBCD

参考代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int N;
    cin>>N;
    char in[N];
    char out[N+2];
    int i=0;char c;
    while(cin>>c){in[i++]=c;}
    in[i]='\0';
    int pos1=0;int pos2=strlen(in)-1;int pos=0;
    while(pos1<=pos2)
    {
    if(in[pos1]<in[pos2])out[pos++]=in[pos1++];
    else if(in[pos1]>in[pos2])out[pos++]=in[pos2--];
    else //如果in[pos1]==in[pos2]
    {
        int t1=pos1;int t2=pos2;
        while(in[t1]==in[t2]&&t1<=t2)
        {
            t1++;t2--;
        }
        if(in[t1]==in[t2])out[pos++]=in[pos1++];
        else if(in[t1]<in[t2])out[pos++]=in[pos1++];
        else out[pos++]=in[pos2--];
    }
    }
    out[pos]='\0';
    int cnt=0;
    while(out[cnt]!='\0')
    {
        cout<<out[cnt];cnt++;
        if(cnt%80==0)cout<<endl;
    }
    return 0;
}

poj 3623(贪心)的更多相关文章

  1. poj 3623 Best Cow Line, Gold 后缀数组 + 贪心

    题目链接 题目描述 对于一个给定的字符串,可以从左右两端取字符,依次排列构成一个新的字符串. 求可能构成的字符串中字典序 最小的一个. 例:ACDBCB -> ABCBCD 思路 参考自 xue ...

  2. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  3. 【POJ 3623】 Best Cow Line, Gold (后缀数组)

    [题意] [分析] 后缀数组水题,嗯,不认真看输出像我一样就会被坑.. #include<cstdio> #include<cstdlib> #include<cstri ...

  4. POJ 2376 贪心

    题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...

  5. poj 1328 贪心

    /* 贪心.... 处理处每个点按照最大距离在x轴上的映射 然后我们就有了一些线段 目的是选取尽量少的点 使得每个线段内都有点出现 我们按照左端点排序 然后逐一处理 假设第一个雷达安在第一个线段的右端 ...

  6. Yogurt factory(POJ 2393 贪心 or DP)

    Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 4197 De ...

  7. Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 ...

  8. POJ - 1456 贪心+并查集

    做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可.时间复杂度O(n * d)当d都为10000时,很容易超时.由于这题数据比较水,所有贪心未超时. AC代码 #include ...

  9. poj supermaket (贪心)

    http://poj.org/problem?id=1456 #include<cstring> #include<iostream> #include<algorith ...

随机推荐

  1. [转帖]ORA-00600: internal error code, arguments: [4193]问题解决

    ORA-00600: internal error code, arguments: [4193]问题解决 https://www.cnblogs.com/linyfeng/p/7496736.htm ...

  2. OpenTSDB在HBase中的底层数据结构设计

    0.时序数据库 时间序列(Time Series):是一组按照时间发生先后顺序进行排列的数据点序列,通常一组时间序列的时间间隔为一恒定值(如1秒,5分钟,1小时等). 时间序列数据可被简称为时序数据. ...

  3. C++ 的关键字(保留字)完整介绍

    转载至:https://www.runoob.com/w3cnote/cpp-keyword-intro.html 1. asm asm (指令字符串):允许在 C++ 程序中嵌入汇编代码. 2. a ...

  4. SSM和Spring Boot常用配置比较

    一.Dao层相关 1.Mysql相关: 1.1配置DataSource连接池: (1)SSM配置: <!-- 加密后配置自己写的解析文件 --> <bean class=" ...

  5. Hadoop组成架构

    Hadoop是apache用来“处理海量数据存储和海量数据分析”的分布式系统基础架构,更广义的是指hadoop生态圈.Hadoop的优势 高可靠性:hadoop底层维护多个数据副本,即使某个计算单元故 ...

  6. Task的取消

    原文:.NET 4 并行(多核)编程系列之三 从Task的取消 .NET 4 并行(多核)编程系列之三 从Task的取消 前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并行编程 ...

  7. hadoop离线数据存储和挖掘架构

    前序: 当你把你知道的东西,写下来,让人看明白是一种境界:当你能把自己写下来的东西给人讲明白,又是另一种境界.在这个过程中,我们都需要历练. 基于hadoop集群下海量离线数据存储和挖掘分析架构: 架 ...

  8. js之运算符(位运算符)

    一.概念 位运算在数字底层(表示数字的32个数位)进行运算的.由于位运算是低级的运算操作,所以速度往往也是最快的,但是它很不直观,许多场合不能够使用.大多数语言都提供了按位运算符,恰当的使用按位运算符 ...

  9. vue配置路由时报错 Error in render: "RangeError: Maximum call stack size exceeded"

    虽然标题写的是配置路由报错,最终也是通过修改路由解决的,但是导致报错的还有一个主要因素,是因为我增加了一个功能“页面刷新时,根据url高亮左侧导航”,如下图: 1.页面刷新,根据url高亮左侧导航代码 ...

  10. 简单易用的字符串模糊匹配库Fuzzywuzzy

    简单易用的字符串模糊匹配库Fuzzywuzzy 阅读目录 FuzzyWuzzy 简介 安装 用法 已知移植 FuzzyWuzzy 简介 FuzzyWuzzy 是一个简单易用的模糊字符串匹配工具包.它依 ...