首页 > 其他分享 >POJ--2386 Lake Counting(DFS)

POJ--2386 Lake Counting(DFS)

时间:2023-01-24 00:55:27浏览次数:61  
标签:his -- Lake DFS int cows new line FJ

记录
0:33 2023-1-24

http://poj.org/problem?id=3617

reference:《挑战程序设计竞赛(第2版)》2.2.3 p43

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

一个贪心算法,书上描述:

需要注意的就是POJ上,这个题要求一行80个字符,多了要换行,不然会presentation error,也就是格式错误。(有种感觉其它题输出也可能要求这样,80个字符,大概是老式terminal?console?啥的一行的字符吧,这里逻辑有点乱。。)

#include<cstdio>
#define MAX_N 10000

int N;
char S[MAX_N + 1];

void solve() {
    // 剩余字符串为S[a], S[a+1], ...., S[b]
    int a = 0, b = N - 1;
    // POJ specification
    // if one line excedd 80 characters, must change new line
    int num = 0;

    while (a <= b) {
        // 左起与右起字符串进行比较
        bool left = false;

        for(int i = 0; a + i <= b; i++) {
            if (S[a + i] < S[b - i]) {
                left = true;
                break;
            }
            else if (S[a + i] > S[b - i]) {
                left = false;
                break;
            }
        }

        if (left) putchar(S[a++]);
        else putchar(S[b--]);

        num++;
        if(num == 80) {
            num = 0;
            putchar('\n');
        }
    }
}

int main() {
    char c;
    scanf("%d\n", &N);
    for(int i = 0; i < N; i++) {
        scanf("%c", &c);
        S[i] = c;
        getchar();
    }
    solve();
    // char c;
    // while (~scanf("%d\n", &N)) {
    //     for(int i = 0; i < N; i++) {
    //         scanf("%c", &c);
    //         S[i] = c;
    //         getchar();
    //     }
    //     solve();
    // }
}

标签:his,--,Lake,DFS,int,cows,new,line,FJ
From: https://www.cnblogs.com/57one/p/17065727.html

相关文章

  • 网络请求HTTP状态码以及含义
    相关文章:python状态码及其含义_Python中HTTP常见响应状态码有几种类型https://blog.csdn.net/weixin_39673704/article/details/111431231PythonRequests库Get和Post......
  • 8、CSS权威指南--第四章(p121)值和单位
     4.1关键字、字符串和其他文本值样式表中的一切都是文本,但是有些类型的值表示的是字符串本省,而不是数字或颜色等。表示字符串本身的值有URL和让人难以置信的图像。4......
  • Git Guide
    Git&Github为什么学习git&github?git是一个代码版本控制系统,github是git版本库托管平台。学习目的:使用git控制项目版本使用github托管项目借助github使用git......
  • uni-app黑马优购项目学习记录
    uni-app黑马优购项目学习记录(上):https://ailjx.blog.csdn.net/article/details/124192676?spm=1001.2014.3001.5502uni-app黑马优购项目学习记录(下):https://ailjx.blog.csdn......
  • Day06-字符串操作
    一、字符串的下标(索引)#获取正负索引数据sub_str=str_data[1] #y#[正索引]0开始取索引的格式 下标 获取单个数据print(sub_str)sub_str=str_data[-2] #o......
  • 力扣---2. 两数相加
    给你两个非空的链表,表示两个非负的整数。它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字。请你将两个数相加,并以相同形式返回一个表示和的链表......
  • [1]-bwapp-文件上传无法保存-问题&解决方案
    一、问题描述1.参考apache2404解决apache2文件配置结构文件无法移动更改权限2.本机环境物理机:windows10虚拟化软件:vmware17虚拟系统:centos7.9docker容器:Do......
  • 数字孪生:打破虚实界限,开发无限可能
    近年来,数字孪生得到了越来越广泛的传播,得益于物联网、大数据、云计算、人工智能等新一代信息技术的发展,数字孪生已经在智慧城市、智慧园区、智能制造等领域沉淀了大量优秀......
  • 推荐一款在浏览器编辑`Blazor`的`IDE`
    不知道是否有Blazor用户羡慕过React或者Vue用户,在一些组件库中,它们就提供了在当前的组件预览对于组件的实时编辑并且预览?比如semi-design的这种在比如codepen这种由......
  • 回忆录
    回忆录前言​ 看过了许多游记,也欣赏过了一些回忆录,心想自己何时也能如他们一般向别人诉说自己或平凡或不凡的故事。巧遇今夜思绪烦乱,回想起自己曾经的经历,便在大年初二夜......