题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个。

析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是说从头部和尾部拿都一样,那么就随便拿一个了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#include <sstream>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e3 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int main(){
while(scanf("%d", &n) == 1){
string s; char ss[5];
for(int i = 0; i < n; ++i){
scanf("%s", ss);
s.push_back(ss[0]);
}
string ans;
for(int i = 0, j = n-1; i <= j; ){
if(s[i] > s[j]) ans.push_back(s[j]), --j;
else if(s[i] < s[j]) ans.push_back(s[i]), ++i;
else{
bool ok = false;
for(int k = i, l = j; k <= l; ){
if(k == l) ans.push_back(s[i]), ++i, ++k, ok = true;
else if(s[k] == s[l]) ++k, --l;
else{
if(s[k] < s[l]) ans.push_back(s[i]), ++i;
else ans.push_back(s[j]), --j;
ok = true;
break;
}
}
if(!ok) ans.push_back(s[i]), ++i;
}
}
for(int i = 0; i < ans.size(); ++i){
if(i && i % 80 == 0) printf("\n");
printf("%c", ans[i]);
}
printf("\n");
}
return 0;
}

POJ 3617 Best Cow Line (贪心)的更多相关文章

  1. POJ 3617 Best Cow Line 贪心算法

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

  2. poj 3617 Best Cow Line 贪心模拟

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42701   Accepted: 10911 D ...

  3. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  4. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  5. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

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

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

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

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

  8. poj 3617 Best Cow Line

    http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...

  9. POJ 3617 Best Cow Line (模拟)

    题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...

随机推荐

  1. window下python 扩展库安装 使用第三方镜像源

    0.前言     由于python的官方镜像位于国外,若使用pip或者easy_install安装第三方插件时或许会被限制,甚至连easy_install或pip也无法安装,例如在windows环境下 ...

  2. RPi 2B Android telnet ssh

    /*********************************************************************** * RPi 2B Android telnet ssh ...

  3. Fragment的知识总结

    1. Fragment概念及作用. 以下是使用Fragment提供思路 2. 创建继承于 Fragment的类:(可extends Fagment 或  ListFagment) 注意导包:如果考虑兼 ...

  4. Heritrix源码分析(五) 如何让Heritrix在Ecplise等IDE下编程启动(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/642550      本博客已迁移到本人独立博客: http://www.yun5u. ...

  5. Maven与Ant的区别

    相同点: Ant和Maven都是基于Java的构建(build)工具. 理论上来说,有些类似于(Unix)C中的make ,但没有make的缺陷.Ant是软件构建工具,Maven的定位是软件项目管理和 ...

  6. 【原创】搭建Nginx(负载均衡)+Redis(Session共享)+Tomcat集群

    为什么移除首页?哪里不符合要求?倒是回我邮件啊! 一.环境搭建 Linux下Vagrant搭建Tomcat7.Java7 二.Nginx的安装配置与测试 *虚拟机下转至root sudo -i 1)下 ...

  7. python numpy argsort函数用法

    numpy.argsort numpy.argsort(a, axis=-1, kind='quicksort', order=None)[source] Returns the indices th ...

  8. aspose.word 查找文本并加下划线

    private Run SplitRun(Run run, int position) { Run beforeRun = (Run)run.Clone(true); beforeRun.Text = ...

  9. HDU5812 Distance 构造,预处理

    分析:怎么看都是超时,但是可以先筛一遍1e6以内的每个数的最小素数 算出每个数由多少个素数组成,然后应用,c[1e6][20] 就是题解的那一套,参照题解,比赛的时候没有想到好的办法筛一个数的因子,醉 ...

  10. saltstack配置安装的一些关键步骤及安装时各种报错的分析

    以下其他仅做参考,官方网址才是安装重点:http://docs.saltstack.cn/topics/installation/rhel.html 与安装相关的一些文档或资料: 一.linux服务器 ...