POJ 3617 Best Cow Line(贪心+最小字典序)
来源:互联网 发布:英文seo教程 编辑:程序博客网 时间:2024/05/21 10:41
原题地址
http://poj.org/problem?id=3617
题意:给定长度为N的字符串S,构造一个新的长度为N的字符串T,使得T的字典序最小。(构造时要么从S的头部删除一个字符加到T的尾部,要么从S的尾部删除一个字符加到T的尾部)
解题思路
本题是简单的贪心算法的题目。最小字典序的题目很多时候可以考虑贪心的思想。
每次观察S最左、最右两端的字符,取较小的字符,如果两端的字符相同,则从两侧同时向中间查看,直到找到一个能比较出大小的字符为止。如果字符全部相同,则任意取左边或右边。
注意输出每80个字符换一行。
AC代码
#include <iostream>#include <cstdio>using namespace std;int main(){ ios::sync_with_stdio(false); int n; cin >> n; char str[2005]; for (int i = 0; i<n; ++i) cin >> str[i]; int a = 0, b = n-1, cnt = 0; while (a <= b) //将从左起和从右起的字符串比较 { bool left = false; //是否取左边 for (int i = 0; a+i <= b; ++i) { if (str[a+i] < str[b-i]) //左端字符小 { left = true; break; } else if (str[a+i] > str[b-i]) //右端字符小 { left = false; break; } } if (left) cout << str[a++]; else cout << str[b--]; ++cnt; if (cnt == 80) //控制输出 { cout << endl; cnt = 0; } } cout << endl; return 0;}
0 0
- POJ 3617 Best Cow Line(贪心+最小字典序)
- POJ 3617 Best Cow Line (字典序最小_贪心)
- POJ 3617 Best Cow Line [贪心] [字典序最小]
- POJ3617 Best Cow Line(贪心,字典序最小)
- POJ 3617 Best Cow Line(贪心,字典序比较)
- POJ 3617 Best Cow Line(字典序最小问题)
- POJ No.3617-Best Cow Line(字典序最小问题)
- 字典序最小问题-贪心-Best Cow Line-POJ3617
- poj 3617 Best Cow Line(取字典序最小的一种排序)
- POJ NO.3617 Best Cow Line(字典序最小问题)
- POJ 3617Best Cow Line(字典序最小的字符串)
- POJ 3617 - Best Cow Line(贪心)
- poj 3617 Best Cow Line(贪心)
- POJ 3617 Best Cow Line(贪心)
- POJ 3617 Best Cow Line (贪心)
- POJ 3617 Best Cow Line (贪心)
- POJ 3617 Best Cow Line(贪心)
- POJ 3617 Best Cow Line(贪心)
- 关于ORACLE blob图片文件的写入
- 原生导航栏控件navigationbar使用说明
- 如何拿到半数面试公司Offer——我的Python求职之路
- Pixhawk原生固件PX4之MAVLink外部通讯
- 写给自己
- POJ 3617 Best Cow Line(贪心+最小字典序)
- for...in 语句
- Android——自定义LinearLayout自动换行,TextView垂直排列。
- Python 爬虫知识汇总 以及40*问题解决策略
- 【干货】机器学习常用35大算法盘点
- SQL基础
- 使用Eclipse搭建Android开发环境
- 2016年(第15届)中国软件业务收入前百家企业名单(zz)
- Xen dom0激活xen_netback dynamic debug (netdev_dbg)