POJ 3617 (贪心)
来源:互联网 发布:淘宝宝贝标题敏感 编辑:程序博客网 时间:2024/05/24 06:23
题意
给出n个字符,现在要求组合成为一个新的字符串。有两个操可以任意选择
1. 从原来的字符串头中拿出一个字母
2. 从原来的字符串尾中拿出一个字母
这道题我又重新做了一遍,我深深地知道自己基础知识掌握的太少、不牢固,现在认认真真的做,每次都要自己思考。
方法:
每次选取字母的时候一定要选取小的(贪心策略),所以可以写一个函数来判断。
要注意的是输出格式要求。满80个字母才能输出换行符。
#include <iostream>#include <cstring>#include <cstdio>using namespace std;int n;char s[10005];int serch(int a,int b){ while(s[a] == s[b] && a <= b) { a++,b--; } if(a == b || s[a] < s[b]) { return true; } else return false;}int main(){ //freopen("t.txt","r",stdin); while(scanf("%d",&n) != EOF) { getchar(); for(int i = 1;i <= n; i++) { scanf("%c",&s[i]); getchar(); } int pos = 0; int a = 1,b = n; for(int i = 0;i < n; i++) { int flag = serch(a,b); if(flag) { printf("%c",s[a++]); pos++; if(pos%80 == 0) printf("\n"); } else { pos++; printf("%c",s[b--]); if(pos%80 == 0) printf("\n"); } } } return 0;}
0 0
- poj 3617 贪心
- poj 3617贪心
- POJ 3617 (贪心)
- POJ 3617 贪心
- POJ 3617 (贪心)
- POJ 3617 3069 贪心
- poj 3617 贪心
- POJ 3617(贪心)
- POJ 3617 字典序贪心
- 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(贪心)
- poj 3617Best Cow Line(贪心)
- POJ 3617 Best Cow Line (贪心)
- springmvc-学习总结-非注解式处理器和映射器
- Gazebo 详细介绍
- leetcode_middle_65_289. Game of Life
- Error: (unix time) try if you are using GNU date
- C++中的string
- POJ 3617 (贪心)
- 终极对决beef之信息提取(4)
- 关于MySQL的TPS和QPS
- 历届试题 格子刷油漆
- Javascript中bind()方法的使用与实现
- ARP地址解析协议
- 无标题Dialog
- [leetcode]167. Two Sum II
- #1475 : 数组分拆