zoukankan      html  css  js  c++  java
  • POJ-3617 Best Cow Line

    题目链接

    https://vjudge.net/problem/POJ-3617

    题目

    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
    

    题意

    给你一个长度为n的序列,每次可以从头或者从尾取出一个字母加到新序列,问新序列字典序最小的序列。

    题解

    贪心的选取,每次选取头或尾字典序较小的加入序列,如果相等的话则比较下一位的大小关系,取较小的加入序列,因为我们要让字典序小的尽量靠前,这样贪心走一遍输出答案即可。注意题目要求每满80个字母要换行

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <string.h>
    #define N 2050
    using namespace std;
    char s[N];
    char ans[N];
    int main() {
    	int n;
    	cin >> n;
    	for (int i = 1; i <= n; i++) {
    		char ch;
    		cin >> ch;
    		s[i] = ch;
    	}
    	bool flag = false;
    	int l = 1, r = n;
    	int cnt = 0;
    	for (int i = 1; i <= n; i++) {
    		flag = false;
    		for (int j = 0; l + j <= r - j; j++) {
    			if (s[l + j] < s[r - j]) {
    				flag = true;
    				break;
    			}
    			else if (s[l + j] > s[r - j]) break;
    		}
    		if (flag) {
    			ans[++cnt] = s[l];
    			l++;
    		}
    		else {
    			ans[++cnt] = s[r];
    			r--;
    		}
    	}
    	int tmp = 0;
    	for (int i = 1; i <= n; i++) {
    		tmp++;
    		cout << ans[i];
    		if (tmp == 80) {
    			tmp = 0;
    			cout << endl;
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    webpack 3.x loader
    git忽略已经被提交的文件,以及如何恢复追踪
    sessionstorage:本地临时存储
    es6中顶层对象属性≠全局属性
    Android DRM
    FFMPEG中关于ts流的时长估计的实现
    整理mp4协议重点,将协议读薄
    LOCAL_SHARED_LIBRARIES 与 LOCAL_LDLIBS,LOCAL_LDFLAGS的区别
    valgrind调查内存leak
    Android中*_handle_t/ANativeWindowBuffer/ANativeWindow/GraphicBuffer/Surface的关系
  • 原文地址:https://www.cnblogs.com/artoriax/p/10373834.html
Copyright © 2011-2022 走看看