poj 3617Best Cow Line
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 题目大意是给你一个字符串s,每回取s的头或尾构成字符串t,使t的字典序最小
用贪心法求解,注意题目要求每行最多输出80个字符
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
char s[], t[];
int n;
int cmp(int a, int b) {
while(s[a] == s[b] && a < b) {
a++;
b--;
}
return s[a] - s[b]; }
void show() {
int len = strlen(t);
int ct = ;
for(int i = ; i < len; i++) {
printf("%c",t[i]);
ct++;
if(ct == ) {
printf("\n");
ct = ;
}
}
printf("\n");
}
int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
scanf("%s",&s[i]);
}
s[n] = '\0';
int ptr = , qtr = n-;
for(int i = ; i < n; i++) {
int cp = cmp(ptr, qtr);
if(cp <= ) {
t[i] = s[ptr];
ptr++;
}
else if(cp > ) {
t[i] = s[qtr];
qtr--;
}
}
t[n] = '\0';
show();
}
return ;
}
poj 3617Best Cow Line的更多相关文章
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- Best Cow Line <挑战程序设计竞赛> 习题 poj 3617
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Goldpoj 3617 http://poj.org/problem?id=3617 题目描述FJ is about ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- POJ 3617:Best Cow Line(贪心,字典序)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30684 Accepted: 8185 De ...
- poj 3617 Best Cow Line (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
随机推荐
- 【持续更新】HTML5 基础知识
文档类型声明 <!DOCTYPE html> 必不可少,位于文件第一行. 字符编码 <meta charset="UTF-8"> 语义化标记元素 heade ...
- javascript for/forEach
基本用法 for:for(var i=0;i<arr.length;i++) forEach:arr.forEach(function(value,index,arr){},),其中functi ...
- 如何加快HTML页面加载速度
1. 页面减肥 a. 页面的肥瘦是影响加载速度最重要的因素. b. 删除不必要的空格.注释. c. 将inline的script和css移到外部文件. d. 可以使用HTML Tidy来给HTML减肥 ...
- 浏览器对DIV+CSS兼容性问题大总结
浏览器对DIV+CSS兼容性问题大总结 接触DIV+CSS架构已经快两年了,个人觉得css入门不难,但要学精并非一朝一夕的,现在大部分网络公司都比较主张用div+css来布局,这就面临着一个比较难的问 ...
- restful十项规范
1.协议 API与用户的通信都是通过HTTPS协议进行的 2.域名 应尽量将API部署在专有域名下:https://api.example.com 如果确定API很简单,不会有什么扩展,则可以放在主域 ...
- OPENFIRE 使用Hazelcast插件进行集群
参考资料:http://www.linuxidc.com/Linux/2014-01/94850.htm https://www.igniterealtime.org/projects/openf ...
- hihoCoder #1068 : RMQ-ST算法(模板)
AC G++ 826ms 146MB 思路: 时间复杂度O(nlogn). //#include <bits/stdc++.h> #include <iostream> #in ...
- codevs 1313 质因数分解
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 已知正整数 n是两个不同的质数的乘积,试求出较大的那个质数 . 输入描述 I ...
- ubuntu14.04 red5
jdk tar -xzvf jdk-8u151-linux-x64.tar.gz nano /etc/profile export HADOOP_HOME=/usr/local/src/hadoope ...
- Nengo 神经网络
Nengo被加拿大滑铁卢大学的神经学家和软件工程师表示,这是迄今为止产生的世界上最复杂.最大规模的人类大脑模型模拟.这个名叫Spaun的大脑由250万 个模拟神经元组成,它能执行8种不同类型的任务.这 ...