前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ3617 Best Cow Line(贪心水题)

POJ3617 Best Cow Line(贪心水题)

作者头像
天道Vax的时间宝藏
发布2021-08-11 10:42:19
2420
发布2021-08-11 10:42:19
举报
文章被收录于专栏:用户5305560的专栏

题目:Best Cow Line

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

代码语言:javascript
复制
6
A
C
D
B
C
B

Sample Output

代码语言:javascript
复制
ABCBCD

代码:

代码语言:javascript
复制
//有一个测试点不通过,求大佬查看帮忙
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<deque>
#include<map>
#include<queue>
#include<iomanip>
#include<string>
#include<stack>
#include<set>
#include<vector>
#include<list>
typedef long long ll;
using namespace std;
int main()
{
	int n;
	cin>>n;
	//stack <char> s1;
	//queue <char> s2;
	char s[2003];
	char s1[2003];
	char s2[2003];
	for(int i=1;i<=n;i++)
	{
		char c;
		cin>>c;
		s1[i]=c;
		s2[n+1-i]=c;
	}
	
	/*for(int i=1;i<=n;i++)
	{
		cout<<s1[i]<<" "<<s2[i]<<endl;
	}*/
	
	/*while(s1.empty()!=true)		
	{
		cout<<s1.top()<<" ";
		s1.pop();						//删除队首元素
	}
	cout<<endl;
	while(s2.empty()!=true)		
	{
		cout<<s2.front()<<" ";
		s2.pop();						//删除队首元素
	}*/
	int p=1,q=1;
	for(int i=1;i<=n;i++)
	{
		if(s1[p]<s2[q]) {//正串首字符小于反串,选正串首字符
			s[i]=s1[p];
			p++;
		}
		else if(s1[p]>s2[q]){//正串首字符大于反串,选反串首字符
			s[i]=s2[q];
			q++;
		}
		else {//如果首字符相同,查看下一个字符
			if(s1[p+1]<=s2[q+1]){//如果下一个字符相同,随便选一个
				s[i]=s1[p];
				p++;
			}
			else if(s1[p+1]>s2[q+1]){
				s[i]=s2[q];
				q++;
			}
		}

		//cout<<p<<" "<<q<<endl; 
		//cout<<s<<endl;
	}
	for(int i=1;i<=n;i++)
	    cout<<s[i];
	//cout<<s1<<" "<<s2<<endl;
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档