原题链接:Best Cow Line

1. 问题描述

2. 输入

6
A
C
D
B
C
B

3. 输出

ABCBCD

4.思路分析

  不断地取原字符串 S 中开头和末尾比较小的字符串放到 T 的末尾

  特殊情况:S 的开头和末尾一样,先放开头的还是结尾的字母。解决办法:将 S 反序排列得到 $S^{'}$ ,与 S 比较,哪个小,就放哪个

5. 代码

#include <iostream>
#include<cstring> using namespace std; int n,i,j;
char s[2002],temp[2002],t[2002]; int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>s[i];
        temp[n-i-1]=s[i];
    }
    for(i=0;i<n;i++)
    {
        if(strcmp(s,temp)<0)
        {
            t[i]=s[0];
            for(j=0;j<n-i-1;j++)
                s[j]=s[j+1];
        }
        else
        {
            t[i]=temp[0];
            for(j=0;j<n-i-1;j++)
                temp[j]=temp[j+1];
        }
        cout<<t[i];
        if((i+1)%80==0)
            cout<<endl;
    }
  return 0;
}

转化成数字数组处理

#include <iostream>
#include <cstring> using namespace std; int n;
char word[2002],t[2002];
int num[2002],temp[2002],c[2002]; int main()
{
    ios::sync_with_stdio(false);
    int i = 0,j = 0;
    cin>>n;
    for(i = 0; i < n; i++)
    {
        cin>>word[i];
        num[i] = word[i] - '0' - 16;
        temp[n-i-1] = num[i];
    }
    for(i = 0; i < n; i++)
    {
        if(memcmp(num,temp,sizeof(num) <= sizeof(temp) ? sizeof(num)/sizeof(int):sizeof(temp)/sizeof(int)) < 0)
        {
            t[i] = num[0] + 16 + '0';
            for(j = 0;j < n-1-i;j++)
                num[j] = num[j+1];
        }
        else
        {
            t[i] = temp[0] + 16 + '0';
            for(j = 0;j < n-1-i;j++)
                temp[j] = temp[j+1];
        }
        cout<<t[i];
        if((i+1)%80 == 0)
            cout<<endl;
    }
    return 0;
}

贪心算法训练(九)——Best Cow Line(字典序最小问题)的更多相关文章

  1. POJ 3617 Best Cow Line 字典序最小

    #include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...

  2. 贪心算法训练(四)——(HDU1050)Moving Tables

    题目描述 在一个狭窄的走廊里将桌子从一个房间移动到另一个房间,走廊的宽度只能允许一个桌子通过.给出 t,表示有 t 组测试数据,再给出 n,表示要移动 n 个桌子.n 下面有 n 行,每行两个数字,表 ...

  3. POJ 3617:Best Cow Line(贪心,字典序)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30684   Accepted: 8185 De ...

  4. POJ 3617 Best Cow Line (字典序最小问题 & 贪心)

    原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...

  5. poj 3617 Best Cow Line (字符串反转贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Des ...

  6. BZOJ 1640 [Usaco2007 Nov]Best Cow Line 队列变换:贪心【字典序最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1640 题意: 给你一个长度为n的字符串. 你可以将原串的首字母或尾字母移动到新串的末尾. ...

  7. POJ 3617 Best Cow Line 贪心算法

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26670   Accepted: 7226 De ...

  8. 编程算法 - 最好牛线(Best Cow Line) 代码(C)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/37909933 最好牛线(Best Cow L ...

  9. poj3617 best cow line(贪心题)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32687   Accepted: 8660 De ...

随机推荐

  1. PyTorch工具

    以装饰器添加所有代码对应的tensor的信息 https://github.com/zasdfgbnm/TorchSnooper

  2. ViewState到底是什么

    ViewState是“客户端”状态保持的一种方案,ViewState是.net中特有的,其他的编程语言,如:Java.PHP中是没有ViewState这个概念的. 举个简单的例子ViewState的例 ...

  3. 使用jQuery开发tree插件

    1.插件截图 2.插件使用 首先引入jquery库,然后引入tree.js.tree.css文件,如下: <script type="text/javascript" src ...

  4. 从MVC -> MVVM ? 开发模式

    MVVM 到底是什么? view :由 MVC 中的 view 和 controller 组成,负责 UI 的展示,绑定 viewModel 中的属性,触发 viewModel 中的命令: viewM ...

  5. 【面试突击】- SpringMVC那些事(一)

    1.什么是Spring MVC ?简单介绍下你对springMVC的理解? Spring MVC是一个基于MVC架构的用来简化web应用程序开发的应用开发框架,它是Spring的一个模块,无需中间整合 ...

  6. JavaScript HTML DOM元素节点常用操作接口

    在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType :节点的类 ...

  7. Java内存模型JMM简单分析

    参考博文:http://blog.csdn.net/suifeng3051/article/details/52611310 http://www.cnblogs.com/nexiyi/p/java_ ...

  8. PHP/Python---百钱百鸡简单实现及优化

    公鸡5块钱一只,母鸡3块钱一只,小鸡一块钱3只,用100块钱买一百只鸡,问公鸡,母鸡,小鸡各要买多少只? 今天看到这题很简单 ,但是随手写出来后发现不是最优的

  9. https和证书小结

    https://www.cnblogs.com/andy9468/p/10484598.html https://www.cnblogs.com/andy9468/p/10414371.html ht ...

  10. MySQL Lock--MySQL加锁学习2

    准备测试数据: ## 开启InnoDB Monitor SET GLOBAL innodb_status_output=ON; SET GLOBAL innodb_status_output_lock ...