题目链接:http://poj.org/problem?id=3617

题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T。构造的规则是,如果S的头部的字母 < S的尾部的字母,那么将S的头部的字母加入到T中,删除S的头部的字母;如果S的头部的字母 > S的尾部的字母,那么将S的尾部的字母加入到T中,删除S的尾部的字母。

  这个题目的关键是如何处理 S 的头部的字母(假设用 i 指示) = S的尾部的字母(j) 这种情况。此时需要比较 i+1 和 j-1 的位置的字母,如果相同,继续比较下去。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; const int maxn = + ;
char s[maxn]; int main()
{
int n, i, j, a, b, cnt;
while (scanf("%d", &n) != EOF)
{
for (i = ; i < n; i++)
{
getchar();
scanf("%c", &s[i]);
}
cnt = ;
i = , j = n-;
while (i <= j)
{
if (s[i] < s[j] && i <= j)
printf("%c", s[i++]);
else if (s[i] > s[j] && i <= j)
printf("%c", s[j--]);
else
{
a = i;
b = j;
while (s[i] == s[j] && i < j)
{
i++;
j--;
}
if (s[i] <= s[j] || i == j) // s[i] <= s[j] 不能写成s[i] < s[j],这是为了处理aaaaa这些情况,否则改了会输出a
{
printf("%c", s[a]);
j = b;
i = a+;
}
else if (s[i] > s[j])
{
printf("%c", s[b]);
i = a;
j = b-;
}
printf("a = %d, b = %d, i = %d, j = %d\n", a, b, i, j);
}
cnt++;
if (cnt % == )
putchar('\n');
}
}
return ;
}

  相反,别人写的,不需要考虑太多琐碎的情况

 #include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; const int maxn = + ;
char s[maxn]; int main()
{
int n, i, a, b;
while (scanf("%d", &n) != EOF)
{
for (i = ; i < n; i++)
{
getchar();
scanf("%c", &s[i]);
}
int cnt = ;
a = , b = n-;
while (a <= b)
{
bool left = false;
for (i = ; a + i <= b; i++)
{
if (s[a+i] < s[b-i])
{
left = true;
break;
}
else if (s[a+i] > s[b-i])
{
left = false;
break;
}
}
if (left)
printf("%c", s[a++]);
else
printf("%c", s[b--]);
cnt++;
if (cnt % == )
putchar('\n');
}
}
return ;
}

poj 3617 Best Cow Line 解题报告的更多相关文章

  1. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  2. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  3. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  4. poj 3617 Best Cow Line (字符串反转贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Des ...

  5. POJ 3617 Best Cow Line 贪心算法

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26670   Accepted: 7226 De ...

  6. poj 3617 Best Cow Line 贪心模拟

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42701   Accepted: 10911 D ...

  7. poj 3617 Best Cow Line

    http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...

  8. 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 ...

  9. POJ 3617 Best Cow Line (字典序最小问题 & 贪心)

    原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...

随机推荐

  1. layui-时间选择器-时间范围选择

    HTML: JS: layui.use(['laydate'],function{ }); start:就为你选择的开始日期; end:就为你选择的结束日期 此方式可选择任意范围的时间,时间格式可任意 ...

  2. Java多线程中Lock的使用

    Jdk1.5以后,在java.util.concurrent.locks包下,有一组实现线程同步的接口和类,说到线程的同步,可能大家都会想到synchronized关键字, 这是java内置的关键字, ...

  3. pt-pmp :pt toolkit

    http://www.cnblogs.com/ivictor/p/6012183.html

  4. utuntu16.04安装caffe+Matlab2017a+opencv3.1+CUDA8.0+cudnn6.0

    上午把tensorflow安装好了,下午和晚上装caffe的确很费劲. 默认CUDA,cuDNN可以用了 caffe官方安装教程 有些安装顺序自己也不清楚,简直就是碰运气 1. 安装之前依赖项 Gen ...

  5. Gvim 和 Opencv编译

    好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...

  6. 使用match、test控制输入字符格式后键盘向左向右键光标自动定位解决

    直接举例说明(正则表达式替换红色区域即可) /** * 判断是否位数字 * @param obj 数字 */ function numOnly(obj) { if(!(/^\d*$/.test(obj ...

  7. 基于Innobackupex的全备恢复

    对于MySQL数据库的热备,xtrabackup是大多数DBA朋友们的选择.xtrabackup内嵌了一个innobackupex可用于热备MySQL数据库.本文描写叙述了基于innobackupex ...

  8. SpringBoot学习之pom文件常见错误

    错误1 上图的错误需要添加jar包的版本号,springboot自动生成不需要添加版本号,手动添加的需要指定版本号. 所以pom.xml如下方式:

  9. 【转载】C#中的==、Equal、ReferenceEqual

    1. ReferenceEquals, == , Equals Equals , == , ReferenceEquals都可以用于判断两个对象的个体是不是相等. a) ReferenceEquals ...

  10. Leetcode题解(4):L216/Combination Sum III

    L216: Combination Sum III Find all possible combinations of k numbers that add up to a number n, giv ...