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

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

代码如下:

#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. PHP全栈工程师学习大纲

    一.高性能网站开发功力提升 时间 标题 内容概要 2015-12-28 开学典礼以及工程师成长路线图 工程师成长的发展路径图.三个阶段,在各个阶段需要提升自己的地方,从技术上也讲了一些提高分析代码的工 ...

  2. Java笔记之String

    1. String s="a",t="b"; t.concat(s); 之后,t仍然是"b",而不是"ba",要使t是& ...

  3. 最大熵模型 Maximum Entropy Model

    熵的概念在统计学习与机器学习中真是很重要,熵的介绍在这里:信息熵 Information Theory .今天的主题是最大熵模型(Maximum Entropy Model,以下简称MaxEnt),M ...

  4. 【英语】Bingo口语笔记(74) - put系列

  5. Hdu 1521 排列组合

    a1 n1 a2 n2 ... ak nkn=n1+n2+...+nk从n个数中选r个排列(不是组合噢)// 指数型母函数// 模板#include <iostream> #include ...

  6. Servlet、Struts2、SpringMVC执行流程

    Servlet 有以下四个阶段: 1.加载和实例化 Servlet容器负责加载和实例化Servlet. 当Servlet容器启动时,或者在容器检测到需要这个Servlet来响应第一个请求时,创建Ser ...

  7. 三 最简单的 AndEngine 程序框架

    package com.example.AndEngineExample; import org.anddev.andengine.engine.Engine;import org.anddev.an ...

  8. 解析AFNetWorking 网络框架(一)

    转:http://blog.csdn.net/andy_jiangbin/article/details/17103041 一直忙于工作,需要资料在网上找到就转发在博客里,今天自己也贡献一点微薄的力量 ...

  9. Team Foundation Server 2010下载安装配置方法

    一.Team Foundation Server 2010下载: ed2k://|file|cn_visual_studio_team_foundation_server_2010_x86_x64_d ...

  10. C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置

    一.对行高,列宽.单元格边框等的设置 这篇简短的文字对单元格的操作总结的比较全面,特此转载过来. private _Workbook _workBook = null; private Workshe ...