POJ 3617 Best Cow Line

本文介绍了一种算法,用于将一组字符按照最小字典序排列。通过删除头部或尾部字符并插入到新字符串中,实现从原始排列到最小字典序排列的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Best Cow Line
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15366 Accepted: 4343

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

Source



题目大意,给一个字符串,只能删除头或尾部的字符,并将其插入新字符串尾部,求产生的字典序最小字符串。


方法:贪心,翻转原字符串,和原串从头比较即可。相同位置的元素就比较这一位后的下一个。


#include <stdio.h>
const int MAXN=2005;
char initialString[MAXN],reverseString[MAXN],ansString[MAXN];
int n;

bool isInitialSmall(int ini,int rev)
{
        if(ini==n||rev==n)
                return true;
        if(initialString[ini]<reverseString[rev])
                return true;
        else if(initialString[ini]>reverseString[rev])
                return false;
        else if(initialString[ini]==reverseString[rev])
                return isInitialSmall(ini+1,rev+1);
}

int main()
{
        while(scanf("%d",&n)>0)
        {
                for(int i=0;i<n;i++)
                {
                        char temp[5];
                        scanf("%s",temp);
                        initialString[i]=temp[0];
                        reverseString[n-i-1]=temp[0];
                }
                int initialPos=0,reversePos=0,cnt=0;
                while(cnt!=n)
                {
                        if(initialString[initialPos]<reverseString[reversePos])
                                ansString[cnt++]=initialString[initialPos++];
                        else if(initialString[initialPos]>reverseString[reversePos])
                                ansString[cnt++]=reverseString[reversePos++];
                        else if(initialString[initialPos]==reverseString[reversePos])
                        {
                                if(isInitialSmall(initialPos,reversePos))
                                        ansString[cnt++]=initialString[initialPos++];
                                else
                                        ansString[cnt++]=reverseString[reversePos++];
                        }
                }
                int c=0;
                for(int i=0;i<n;i++)
                {
                        printf("%c",ansString[i]);
                        c++;
                        if(c%80==0&&i<n-1)
                                printf("\n");
                }
        }
        return 0;
}


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值