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 ...
随机推荐
- CF1060C Maximum Subrectangle
思路: 不难发现,对矩阵中的数字求和实际上是先分别对a,b两个数列中对应子段的元素求和再相乘.题目是要求在和不超过给定值的情况下使选出的矩阵面积最大.我们反其道而行之,考虑在子段长度一定的情况下,和最 ...
- Visual Studio使用技巧学习
F7: 代码窗口 Shift+F7: 对象窗口 F4: 属性窗口 闪电图标: 对象的事件 F5: 编译及运行 Ctrl+F5: 编译及运行(不调试) svm+两次Tab: s ...
- arcgis jsapi接口入门系列(5):几何(点线面)基本操作
点 point: function () { //通过wkt生成点 //wkt,代表点的坐标 let wkt = "POINT(113.566806 22.22445)"; //w ...
- Dll加载总是出问题,显示无法加载
我从网上找了一个类似的问题,具体的内容如下 创建了个mfc的共享链接库,里面只有这样一个加法 _declspec(dllexport) int add(int a,int b){ return a+b ...
- 破解MySQL和修改mysql的密码
/etc/init.d/mysql stop mysqld_safe --user=mysql --skip-grant-tables --skip-networking & mysql -u ...
- IOS小技巧整理
1 随机数的使用 头文件的引用 #import <time.h> #import <mach/mach_time.h> srandom()的使用 ...
- 【Web应用-Web作业】Web 作业无法直接运行 jar 文件
问题描述 在经典管理门户中将直接压缩的 jar 文件打包为 zip 包,上传到 web 作业时报错. 解决方法 jar 文件的运行需要依托于 java 进程,所以在运行 jar 文件时,我们都会以格式 ...
- java 核心技术卷一笔记 6 .2接口 lambda 表达式 内部类
6.2 接口实例 6.2.1 接口与回调 在java.swing包中有一个Timer类,可以使用它在到达给定的时间间隔时发出通告,假如程序中有一个时钟,就可以请求每秒钟获得一个通告,以便更新时钟的表盘 ...
- make 与makefile(会不会写 makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。)
跟我一起写 Makefile /**/ 陈皓 (CSDN) 概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉 ...
- Gym 100883J palprime(二分判断点在凸包里)
题意:判断一堆小点有多少个在任意三个大点构成的三角形里面. 思路:其实就是判断点在不在凸包里面,判断的话可以使用二分来判断,就是判断该点在凸包的哪两个点和起点的连线之间. 代码: /** @xigua ...