Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27635   Accepted: 7438

Description

FJ is about to take his N (1 ≤ N ≤ 2,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

Source

USACO 2007 November Silver



问题链接:POJ3617 Best Cow Line。

问题简述

输入一个正整数N,再输入N行,每行包含一个字母('A'-'Z'),将这些字母按顺序构成一个字符串S。按照以下规则取字符,顺序构成一个新的字符串T,T是按照字典顺序最小的。

1.从S的头取一个字符并且删除该字符,连接到T中(开始时T为空串);

2.从S的尾取一个字符并且删除该字符,连接到T中。

问题分析

这是用一个字符串构建另外一个字符串的问题。比较S的首字符和尾字符,取其小连接到T中即可;当首尾字符相同时,则需要比较下一个,尽量取下一个较小的;当S是一个回文串时,则从哪边取字符结果都是一样的。

程序说明

使用一个函数test()来比较哪边更小,是一个最为有效的方法,可以使得程序逻辑更加简洁。需要注意的一点是,输出时,每行最多输出80个字符,即每80个字符输出一个换行。

比起使用排序的程序,这个程序要快速一些。

AC的C++语言如下:

/* POJ3617 Best Cow Line */#include <iostream>using namespace std;//#define DEBUGconst int LEFT = 1;
const int RIGHT = 2;const int MAXN = 2000;
char a[MAXN+1];int test(char s[], int start, int end)
{while(start < end) {if(s[start] < s[end])return LEFT;else if(s[start] > s[end])return RIGHT;start++;end--;}return LEFT;
}int main()
{int n;cin >> n;for(int i=0; i<n; i++)cin >> a[i];a[n] = '\0';#ifdef DEBUGcout << a << endl;
#endifint start=0, end=n-1, count=0;for(int i=0; i<n; i++) {if(test(a, start, end) == LEFT)cout << a[start++];elsecout << a[end--];if(++count == 80) {count = 0;cout << endl;}}return 0;
}

POJ3617 Best Cow Line【水题】相关推荐

  1. Best Cow Line(POJ-3617)

    Problem Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the ...

  2. P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希)

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希) 洛谷上这道水题丧心病狂地把数据范围加到了500000 普通的做法肯定A不了了, ...

  3. 图论刷水题记录(一)(最短路-----dijkstra算法)

    最近实在不知道干些什么,感觉自己除了水题什么都不会做,算了去刷一刷图论的水题吧本来想合起来一起发,想了想太长的话以后看起来也不方便,题目所以今天晚上就先发了dij部分,由上到下由易变难. 1.POJ ...

  4. 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...

  5. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  6. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题

    B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...

  7. Educational Codeforces Round 7 B. The Time 水题

    B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...

  8. NUC1373 Bank Interest【水题】

    Bank Interest 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 Farmer John made a profit last year! H ...

  9. hdu-5003 Osu!(水题)

    题目链接: Osu! time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Prob ...

最新文章

  1. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)...
  2. pip不是内部 pycharm_PyCharm的简单使用
  3. 【问题】windows网络问题快速诊断方法或工具。终于调通了MQTT,论坑爹的windows防火墙!...
  4. Android Gradle使用总结
  5. 企业云上安全事件突发,这五个问题值得运维大佬们日常自查!
  6. 计算机音乐除了春天爱情和樱花,High4IU《除了春天爱情和樱花》新歌试听
  7. Cloud for Customer UI designer自动生成的一些模型
  8. 状态模式和策略模式的区别
  9. js转换字符串为base64位
  10. python环境问题(pycharm)
  11. 如何爬取html和样式,如何批量爬取多个网页上不同样式的表格?
  12. mybatis-plus 从2.x到3.x升级指南
  13. MySQL设置字符编码
  14. 德语翻译器在线翻译中文-德语翻译器支持各大语言翻译
  15. cocos 修改层级_管理节点层级和显示顺序
  16. Ubuntu 16.04安装搜狗拼音
  17. C语言求田径场周长和面积,田径场尺寸计算及画法
  18. linux打开xml文件,查看 XML 文件
  19. 数据结构01秦九算法
  20. cordova跳转远程页面调用浏览器问题

热门文章

  1. Modelbuilder进阶教程
  2. b站测试岗怎么样_情商测试《大家一起察言观色》,一款适合作为B站测试题的游戏...
  3. HIVE 的笛卡尔积on无条件
  4. spring的部分注释
  5. linux定义数据结构压缩,Linux下数据结构学习笔记
  6. python第七章_Python第七章
  7. LeetCode 230. 二叉搜索树中第K小的元素(递归)
  8. LeetCode 10.正则表达式匹配(动态规划)
  9. mysql innodb_file_format_innodb_file_format设置
  10. Django合并多个查询结果