POJ-3617 Best Cow Line(贪心法)
2015-06-14 09:34
246 查看
反复的比较正序列的字典序和反序列的字典序,如果字典序相同,跳过,如果全为相同的字母,就特殊处理一下。
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
Sample Output
Source
USACO 2007 November Silver
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
#include<cstdio> #include<cmath> #include<string> #include<cstdlib> #include<iostream> #include<cstring> using namespace std; int main() { string str1, str2 ,s; int n; int cnt = 0; cin>>n; for(int i = 0; i < n ; i++) { cin>>s; str1 += s; } int a = 0, b = n - 1; while(a <= b) { // cout<<a<<b<<endl; if(a == b) { str2+=str1[a]; break; } for(int i = 0 ; a + i <= b ;i++) { if(str1[a + i] > str1[b - i]) { str2 += str1[b]; b--; break; } else if(str1[a + i] < str1[b - i]) { str2 += str1[a]; a++; break; } else if(a + i == b) { str2 += str1[a]; a++; break; } } } for(string::iterator it = str2.begin(); it < str2.end() ; it++) { cout<<*it; cnt++; if(cnt % 80 == 0) cout<<endl; } return 0; }
相关文章推荐
- 菜鸟学Andoid笔记(三十):Response实现重定向和注意事项
- hdu(1754)——I hate it(更新节点,区间最值)
- Xshell远程连接Ubuntu
- 3. mysql数据类型
- MySql 插入 读取数据正常,工具看到乱码。
- 学习杂记linux
- CentOS7 Apache+Mysql+PHP+Memcached安装
- 化抽象为具体学动态规划
- 用例建模(设计)
- 2.1. sql增删查改
- UV镜还是保护镜?差异很大啊!(附14款镜片测试结果)
- bash array
- Java中getMessage()和printStackTrace方法
- MyBatis数据持久化(一)准备工作
- MyBatis数据持久化(一)准备工作
- 使用JDBC连接MySQL数据库--典型案例分析(一)---员工数据读取与操作
- MyBatis数据持久化(一)准备工作
- 2. mysql 基本命令
- 【Java】定时器、线程与匿名内部类
- 1. mysql安装