POJ 3617 - Best Cow Line(贪心)
来源:互联网 发布:sql 数值转化为字符串 编辑:程序博客网 时间:2024/06/08 06:55
题目:
FJ is about totake his N (1 ≤ N ≤ 2,000)cows to the annual"Farmer of the Year" competition. In this contestevery farmer arranges his cows in a line and herds them past the judges.
The contestorganizers adopted a new registration scheme this year: simply register theinitial letter of every cow in the order they will appear (i.e., If FJ takesBessie, Sylvia, and Dora in that order he just registers BSD). After theregistration phase ends, every group is judged in increasing lexicographicorder according to the string of the initials of the cows' names.
FJ is very busythis year and has to hurry back to his farm, so he wants to be judged as earlyas possible. He decides to rearrange his cows, who have already lined up,before registering them.
FJ marks alocation for a new line of the competing cows. He then proceeds to marshal thecows from the old line to the new one by repeatedly sending either the first orlast 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 initialorder of his cows, determine the least lexicographic string of initials he canmake this way.
题意:
将字符串重新排列,排成最小字典序,只能从最前或最未取字符。
Thinking:
将最前与最末字符进行比较,取小的,若相同,则比较里面一层的字符,直至比出大小,在小的一边取数。
若相等,要用循环,写得我半死,结果是将--写成++了;还有最后输出格式忽略了i从0开始,还有n==0的情况!!
代码:
#include <iostream>#include <cstdio>using namespace std;char s[2005], ans[2005];int main(){//freopen("in", "r", stdin); int n; while(~scanf("%d", &n)) { for(int i = 0; i < n; ++i) { getchar(); scanf("%c", &s[i]); } int i = 0, j = n - 1, k = 0; while(i <= j) { if(s[i] < s[j]) { ans[k++] = s[i]; i++; } else if(s[i] > s[j]) { ans[k++] = s[j]; j--; } else { int ii = i, jj = j;// while(1) {// if(ii >= jj) {// for(int t = i; t <= j; ++t) {// ans[k++] = s[t];// }// i = ii+1;// j = jj-1;// break;// }// else if(s[ii] > s[jj]) {// for(int t = j; t >= jj; --t) {// ans[k++] = s[t];// }// j = jj-1;// break;// }// else if(s[ii] < s[jj]) {// for(int t = i; t <= ii; ++t) {// ans[k++] = s[t];// }// i = ii+1;// break;// }// ii++; jj--;// } while(ii <= jj && s[ii] == s[jj]) { ii++; jj--; } if(s[ii] < s[jj]) { ans[k++] = s[i]; i++; } else if(s[ii] > s[jj]) { ans[k++] = s[j]; j--; } else { for(int t = i; t <= j; ++t) { ans[k++] = s[t]; } i = j + 1; } } } for(int i = 0; i <= n; ++i) { if(i % 80 == 0 && i != 0) { puts(""); if(i == n) break; } else if(i == n) { printf("\n"); break; } printf("%c", ans[i]); } } return 0;}
- 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(贪心)
- POJ 3617 Best Cow Line 贪心
- POJ 3617 Best Cow Line(贪心)
- poj 3617Best Cow Line(贪心)
- 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(贪心)
- 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 - 贪心
- [ios]图片转pdf 【转】
- [ios]Objective-C 和 Core Foundation 对象相互转换的内存管理总结 【转】
- [ios]总结iOS开发当中一些特别注意的问题 【转】
- [资源] iphone开发资源汇总 http://blog.csdn.net/favormm/article/details/6664970
- [ios]The identity 'iPhone Developer' doesn't match any valid, non-expired certifi错误解决方法
- POJ 3617 - Best Cow Line(贪心)
- [资源] UITableView效果集
- [ios]添加第三方类库造成的linker command failed with exit code 1 (use -v to see invocation)的错误调试 【转】
- [ios]音频(录音),视频 基础
- [ios]获取时间 【转】
- [ios] UILocalNotification实现本地的闹钟提醒【转】
- [ios] IOS 应用之间的调用【转】
- [ios] iOS图片相关(读取、保存、绘制、其它相关) 【转】
- 山寨程序员的苦恼