POJ 3617 字典序贪心
2017-12-10 23:31
260 查看
Best Cow Line
DescriptionFJ 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 phaseends, 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'sfinished, 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 lineOutputThe 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
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27553 | Accepted: 7416 |
6 A C D B C BSample Output
ABCBCD
题意:给一个长度为N的字符串s,然后对s进行如下操作:
比较s的头和尾的字符的字典序大小,小的输出,并删除此位置的字符,直到s为空。
题解:首先贪心是肯定的,依次比较头和尾的字符的字典序大小,小的直接输出,但如果比较的两个字符是相等的呢?
比如这个例子: abacba,它的答案是:aababc,那么该如何比较呢?做法是将s和反转s后的s'进行比较。
s :abacba
s':abcaba
设a为s串的起始位置,b为反串的起始位置(为s的最后一位的位置)
依次比较s和s'的相同位置的字符的大小,小的直接输出,相同就继续比较,如果s的某一位置的字符更小,那么就输出s的s[a],
否则就输出s[b],这里设置一个bool型的up,s小up=true,否则up=false。
AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int n; cin >> n; string s=""; while(n--) { string tmp; cin >> tmp; s+=tmp; } int cnt=0; int a=0,b=s.length()-1; while(a<=b) { bool up=false; for(int i=0;a+i<b;i++) { if(s[a+i]<s[b-i]) { up=true; break; } else if(s[a+i]>s[b-i]) { up=false; break; } } cnt++; if(up) putchar(s[a++]); else putchar(s[b--]); if(cnt==80) {cout << endl;cnt=0;} //输出要求一行80个字符,坑点。。。 } cout << endl; return 0;}
相关文章推荐
- poj-3617字典序最小 贪心
- poj-3617字典序最小 贪心
- poj-3617字典序最小 贪心
- POJ 3617 Best Cow Line (字典序最小_贪心)
- poj-3617字典序最小 贪心
- poj-3617字典序最小 贪心
- POJ 3617 Best Cow Line [贪心] [字典序最小]
- POJ 3617 Best Cow Line(贪心+最小字典序)
- 字典序最小问题 前后删除加入 贪心poj3617
- Poj 3617 Best Cow Line【贪心】
- POJ:3617-Best Cow Line(贪心)
- POJ 3617 3069 贪心
- POJ 3617 贪心
- poj 3617 Best Cow Line(简单贪心)
- poj 3617字典序最小问题
- POJ 3617 Best Cow Line(贪心)
- POJ 3617 Best Cow Line 【贪心】
- POJ 3617 Best Cow Line——贪心
- POJ 3617 Best Cow Line(贪心)
- POJ 3617 Best Cow Line(贪心)