poj3617 贪心
来源:互联网 发布:共产主义社会 知乎 编辑:程序博客网 时间:2024/06/10 15:41
题意:给定长度为N的字符串S,要构造一个长度为N的字符串T。起初,T是一个空串,随后反复进行下列任意操作:
从S的头部删除一个字符,加到T的尾部
从S的尾部删除一个字符,加到T的尾部
目标是使T字典序最小,输出时每行最多80个字符。
从S的头部删除一个字符,加到T的尾部
从S的尾部删除一个字符,加到T的尾部
目标是使T字典序最小,输出时每行最多80个字符。
算法:贪心。每次选择S头部或尾部最小的字符,如果两者相同,继续比较下一个字符,直到找到较小的字符。
#include <iostream>using namespace std;int N;char c[2010];void solve(){int l = 0;int r = N - 1;bool left, right;left = true;int cnt = 0;while (l <= r){int i;for (i=0; i+l<r; i++){if (c[i+l] > c[r-i]){left = false;break;}else if (c[i+l] < c[r-i]){left = true;break;}}left? cout << c[l++] : cout << c[r--];cnt++;if (cnt % 80 == 0){cout << endl;}}if (cnt % 80 != 0){cout << endl;}}int main(){cin >> N;for (int i=0; i<N; i++){cin >> c[i];}solve();}
0 0
- 贪心--poj3617
- poj3617 贪心
- poj3617贪心
- poj3617 贪心
- poj3617【贪心】
- poj3617贪心,易错
- poj3617 Best Cow Line 贪心
- POJ3617 Best Cow Line 贪心
- poj3617 Best Cow Line 贪心
- poj3617字典序问题【贪心】
- POJ3617-贪心字典序 水
- poj3617 Best Cow Line 贪心
- poj3617 Best Cow Line【贪心】
- POJ3617 Best Cow Line (贪心)
- 【poj3617】Best Cow Line 贪心
- 贪心字典序最小问题poj3617
- poj3617 Best Cow Line (贪心)
- poj3617 Best Cow Line(贪心+字符串)
- poj 3522
- jquery 实现导航栏滑动效果
- hiho 1290 2016微软4月笔试 dp
- [HDOJ 1180]深度优先搜索 vs. 广度优先搜索
- 打包成jar文件后运行出现Invalid or corrupt jarfile 解决
- poj3617 贪心
- 广度优先搜索
- bower详细教程
- Server Tomcat v7.0 Server at localhost failed to start.
- 【C++标准I/0库】错误:runtime_error was not declared
- 算法之归并排序
- 【湖南集训 4.7】sanrd
- STL学习笔记— —容器forward_list
- 并查集——How Many Tables