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 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...
随机推荐
- DELPHI是怎么实现跨平台的?
DELPHI是怎么实现跨平台的? 首先跨平台必须要兼容原来的语法,以线程的临界区对象为例: TCriticalSection = class(TSynchroObject){$IFDEF POSIX} ...
- Git学习0基础篇(下)
server上的 Git - 协议 Git能够使用四种基本的协议传输资料:本地协议(Local).HTTP 协议.SSH(Secure Shell) 协议以及 Git 协议.眼下使用最普及的是 SSH ...
- python 中的电子邮箱的操作
通过python 的代码实现对email的操作,包括发送邮件和读取邮件. import poplib import smtplib from email.header import decode_he ...
- vs 编译错误 The name 'InitializeComponent' does not exist in the current context in WPF application
1:文件命名空间的问题 xaml文件和model.cs文件的命名空间 2:csproj 那么它究竟是给谁用的呢?那是给开发工具用的,例如我们在熟悉不过的Visual Studio,以及大家可以没有接触 ...
- 基于multiprocessing和threading实现非阻塞的GUI界面显示
========================================================= 环境:python2.7.pyqt4.eric16.11 热点:multiproce ...
- bootstrap table api
http://blog.csdn.net/rickiyeat/article/details/56483577
- Spring Boot外部化配置实战解析
一.流程分析 1.1 入口程序 在 SpringApplication#run(String... args) 方法中,外部化配置关键流程分为以下四步 public ConfigurableAppli ...
- Java爬虫快速开发工具uncs的部署攻略
写在前面 uncs是java快速开发爬虫的工具,简单便捷,经过大量版本迭代和生产验证,可以适用大多数网站,推荐使用. 一.基本用法 1.1 开发包获取 目前只能在公司内网maven服务器获取到 < ...
- LeetCode215:Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- Operation not permitted - /usr/bin/pod
问题描述:执行sudo gem install cocoapods, 提示出错:While executing gem ... (Errno::EPERM) Operation not per ...