poj 3617 Best Cow Line 解题报告
题目链接: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 解题报告的更多相关文章
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- poj 3617 Best Cow Line (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- poj 3617 Best Cow Line
http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...
- 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 ...
- POJ 3617 Best Cow Line (字典序最小问题 & 贪心)
原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...
随机推荐
- 使用JAXP对xml文档进行DOM解析基础
XML解析方式分为两种:dom和sax dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种方式. sax: ...
- datetimepicker使用总结
datetimepicker使用总结 2019-03-06 16:55:00 使用效果: 官方教程:http://www.bootcss.com/p/bootstrap-datetimepick ...
- rocketMq---------相关命令
搭建就不详细说了,cent7.x的系统,openJdk8,maven3.x,gradle4.10.2, git 1.8.3.1 直接下载相关的二进制压缩包,解压即用,方便. 下面看常用的管理命令 ro ...
- uitableview执行deleteRowsAtIndexPaths时出错
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid updat ...
- The bean 'xxx' could not be injected as a 'xxx'because it is a JDK dynamic proxy that implements
启动springboot项目的时候示以下错误 Error starting ApplicationContext. To display the conditions report re-run yo ...
- 【翻译自mos文章】使用asmcmd命令在本地和远程 asm 实例之间 拷贝asm file的方法
使用asmcmd命令在本地和远程 asm 实例之间 拷贝asm file的方法 參考原文: How to Copy asm files between remote ASM instances usi ...
- 记事本中写c/c++程序在Windows下运行
1.在桌面新建一个1.c,内容例如以下: 2.打开cmd命令行窗体,进入CodeBlock安装文件夹下的:E:\Installed\CodeBlocks\MinGW.然后运行mingwvars. ...
- mac os PHP 访问MSSQL
写在前: 项目的数据库是sql server,但是自己的系统是mac os.这样导致了需要一个烦人的系统环境搭建过程.目前要在mac 上的php环境中支持mssql环境访问,经过自己了解,有两种方式: ...
- ElasticSearchserver操作命令
在win7环境,进入elasticsearch安装文件夹的bin文件夹: 1. elasticsearch.bat 就能够启动elasticsearch了.执行这个插件的优点是:elasticsear ...
- 杭电 HDU 1279 验证角谷猜想
验证角谷猜想 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...