poj3617 Best Cow Line【贪心】
来源:互联网 发布:淘宝上耐克是正品吗 编辑:程序博客网 时间:2024/06/05 23:43
题目链接:http://poj.org/problem?id=3617
题意:给你一个长度为n的字符串S,让你用这个字符串构建一个新的字符串T,构建过程可以反复使用一下任意一种操作
将S的第一个元素删除,放在T的尾部
将S的最后一个元素删除,放在T的头部
让你尽可能的使得T的字典序最小
解析:每次从S拿元素时,只要保证拿到的最小即可,如果头尾选择的元素相等则比较他们下一位,直至比出大小为止
注意一个坑点,就是输出每行只能80个字符,不然会PE
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;const int maxn = 2005;char a[maxn];int main(void){ int n; scanf("%d",&n); getchar(); for(int i=0;i<n;i++) { scanf("%c",&a[i]); getchar(); } int l=0,r = n-1; int cnt = 0; while(l<=r) { int t1=l,t2=r; bool lb=false; while(a[t1]==a[t2]) { if(a[t1+1]<a[t2-1]) { lb = true; break; } else { t1++; t2--; } } if(lb || a[l]<a[r]) { printf("%c",a[l]); l++; } else { printf("%c",a[r]); r--; } cnt++; if(cnt==80) { puts(""); cnt = 0; } } if(cnt!=0) puts(""); return 0;}
0 0
- poj3617 Best Cow Line 贪心
- POJ3617 Best Cow Line 贪心
- poj3617 Best Cow Line 贪心
- poj3617 Best Cow Line 贪心
- poj3617 Best Cow Line【贪心】
- POJ3617 Best Cow Line (贪心)
- 【poj3617】Best Cow Line 贪心
- poj3617 Best Cow Line (贪心)
- poj3617 Best Cow Line(贪心+字符串)
- POJ3617 Best Cow Line
- POJ3617 Best Cow Line
- POJ3617 Best Cow Line
- best cow line(poj3617)
- POJ3617-Best Cow Line
- POJ3617 Best Cow Line
- POJ3617 Best Cow Line
- poj3617 Best Cow Line
- POJ3617 Best Cow Line
- Android WebView重定向GOBACK问题
- Leonard Cohen --- I am your Man 歌词意译
- 快速排序
- 应用setrlimit控制进程使用系统CPU
- 读写效率
- poj3617 Best Cow Line【贪心】
- Fast Food hdu 1227 dp动态规划
- 110. Balanced Binary Tree | 判断是否是平衡二叉树
- 网络中的MTU
- OpenCV3.0+MFC+VS2010打开图像
- 快速N次方练习
- mysql的用户管理
- FastDFS-分布式文件系统安装(单节点)
- UVA-1584 Circular Sequence