BEST COW LINE(贪心)

BEST COW LINE(贪心)

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
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 Output

ABCBCD

问题简述:

输入一个正整数N,再输入N行,每行包含一个字母(‘A’-‘Z’),将这些字母按顺序构成一个字符串S。按照以下规则取字符,顺序构成一个新的字符串T,T是按照字典顺序最小的。

1.从S的头取一个字符并且删除该字符,连接到T中(开始时T为空串);

2.从S的尾取一个字符并且删除该字符,连接到T中。

问题分析:

这是用一个字符串构建另外一个字符串的问题。比较S的首字符和尾字符,取其小连接到T中即可;当首尾字符相同时,则需要比较下一个,尽量取下一个较小的;当S是一个回文串时,则从哪边取字符结果都是一样的。

程序说明:

使用一个函数test()来比较哪边更小,是一个最为有效的方法,可以使得程序逻辑更加简洁。需要注意的一点是,输出时,每行最多输出80个字符,即每80个字符输出一个换行。

比起使用排序的程序,这个程序要快速一些。

代码实现

=/* POJ3617 Best Cow Line */

#include <iostream>

using namespace std;

//#define DEBUG

const int LEFT = 1;
const int RIGHT = 2;

const int MAXN = 2000;
char a[MAXN+1];

int test(char s[], int start, int end)
{
  while(start < end) {
      if(s[start] < s[end])
          return LEFT;//***直接return//
      else if(s[start] > s[end])
          return RIGHT;

      start++;
      end--;
  }

  return LEFT;//start==end也都一样return left//
}

int main()
{
  int n;

  cin >> n;
  for(int i=0; i<n; i++)
      cin >> a[i];
  a[n] = '\0';//字符数组赋值要用最后末位补零//

#ifdef DEBUG
  cout << a << endl;
#endif

  int start=0, end=n-1, count=0;
  for(int i=0; i<n; i++) {
      if(test(a, start, end) == LEFT)
          cout << a[start++];
      else
          cout << a[end--];

      if(++count == 80) {
          count = 0;
          cout << endl;
      }
  }

  return 0;
}

问题描述

题目大意:一个长度为N的字符串,可以从字符串头部删除一个元素,也可以从字符串尾部删除一个元素,加到一个新的字符空串的尾部,使得新的字符串字典序最小。

只需要每次判断字符串头部是否小于尾部然后决定删除哪一个元素即可,然后对于头尾相同的,不断循环比较下一个字符。

代码实现(自己的)

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int n;
    cin>>n;
    char ch;
    vector<char>vec;
    for(int i=0;i<n;i++)
    {
        cin>>ch;
        vec.push_back(ch);
    }
    int start1=0,end1=n-1;
    int cont=0;
    while(start1<=end1)
    {   bool LEFT=true;
        int kk=start1,mm=end1;
        while(kk+1<mm-1&&vec[kk]==vec[mm])//分总长度为奇数最后重合总长度为偶数最后停止//
        {
            kk++;mm--;
        }
        if(vec[mm]<vec[kk])//确定什么时候改因为相等时没必要改//
            LEFT=false;
            if(LEFT)cout<<vec[start1++];
            else  cout<<vec[end1--];
            cont++;
            if(cont%80==0)
                cout<<endl;
    }
    cout<<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值