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 ...
随机推荐
- bootstrop媒体对象、面板和Well
媒体对象 <div class="media"> <div class="media-left"> < ...
- bootstrap输入框组、导航和导航条
输入框组(input groups) 避免使用select 支持不好,使用输入框组 尺寸根据 input-group-lg input-group-sm来选择 <div class ...
- Javafinal变量
class Test02 { public static void main(String args[]){ final int x; x = 100; // ...
- (十)mybatis之配置(mybatis-config.xml)
配置 可以从前篇的文章中知道(https://www.cnblogs.com/NYfor2018/p/9093472.html ),要使用mybatis需要以下配置: 1. mybatis-con ...
- nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati
root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx --add-mod ...
- maven打包错误:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] Scanning for projects...[INFO] ...
- ubuntu 14.04安装 nginx直播服务平台
在官网上下载nginx,可以选中直接从ubuntu的源红直接安装:sudo apt-get install nginx.还有就是源码编译安装,我选择的是源码编译安装.具体的步骤如下: ll /usr/ ...
- leetcode 127 单词接龙
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...
- shell脚本,100以内的质数有哪些?
[root@localhost wyb]# cat 9zhishu.sh #!/bin/bash ` do ;j<=i-;j++)) do [ $((i%j)) -eq ] && ...
- Spring持久化之MyBatis
MyBatis是一个优秀的轻量级持久化框架,本文主要介绍MyBatis与Spring集成的配置与用法. 1. Spring MyBatis配置 1.1 添加Maven依赖 在pom.xml文件里添加m ...