Best Cow Line (POJ 3217)

给定长度为N的字符串S,要构造一个长度为N的字符串T,起初,T是一个空串,随后反复进行下列任意操作。

*从S的头部删除一个字符,加到T的尾部

*从S的尾部删除一个字符,加到T的尾部

目标是要构造字典序尽可能小的字符串T

模拟情景,想出来一个有意思的比喻,写出来程序,俩个推土机开始在互相看不到对方的直线工作推物品,每个人每次走一步,并且先将物品重量较小的先推走, 由于没有手机,只能通过副驾驶通信员互相沟通,谁的前方物品较轻先推走,于是通信员下车检查,物品,当A,B通信员发现一方较轻时,就全部回各自的车报告向前走一步或者不走。但是通信员发现前方各自的第一个物品重量相同,就各自继续向前走一步直到发现重量较轻的返回各自的车内,报告走一步后者是不走。当通信员发现前方各自的重量都是一样的就对对方说,我们谁先走都行。互相谦让,选出一个通信员,回到车内,向前走一步。 直到推掉所有的物品,俩个车相见完成任务。

#include<stdio.h>
const int MAX=100;
int N;
char S[MAX+1];
void f(){
int a=0,b=N-1;
int left;
while(a<=b){//当俩个推土机还没有遇见的时候
for(int i=0;a+i<=b;i++){ //A/B通信员下车报告
if(S[a+i]<S[b-i]){ //A通信员发现前方物品重量较小
left=1;    break;
}else if(S[a+i]>S[b-i]){ //B通信员发现前方物品较小
left=0; break;
}
}
if(left) printf("%c",S[a++]); //推土机向前开始工作
else    printf("%c",S[b--]);
}
}
int main(){
while(scanf("%d",&N)){
//一定注意接收上一个字符
getchar();
for(int i=0;i<N;i++){
scanf("%c",&S[i]);
}
f();
}
return 0;
}
时间: 2024-12-15 04:17:09

Best Cow Line (POJ 3217)的相关文章

Best Cow Line (POJ 3617)

题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到T的尾部 目标是要构造字典序尽可能小的字符串T. 1 #include "iostream" 2 #include "cstring" 3 #include "vector" 4 5 using namespace std; 6 #define MAX_N 1000 7

POJ 3623 Best Cow Line, Gold(模拟)

题意  给你一个字符序列   你每次可以从它的头部或尾部拿出一个字符组成一个新的字符序列   输出这样做能达到的最小的字符序列   每行最多输出80个字符(开始被这个坑了好久) 直接模拟就行  哪边小就选哪边  相等就往内看 #include<cstdio> #include<iostream> #include<string> using namespace std; const int N = 30010; int main() { char s[N][2]; in

POJ 3623 Best Cow Line, Gold(字符串处理)

题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再相等就继续.... 所以比较这个动作的单一功能,可以写成一个check函数,方便操作也方便递归. #include<iostream> #include<cstring> using namespace std; #define MAX 30005 char str[MAX]; int

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

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his

poj 3617 Best Cow Line

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20226   Accepted: 5563 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his

【POJ 3617】Best Cow Line

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13950   Accepted: 3987 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his

POJ 题目3623 Best Cow Line, Gold(后缀数组rank简单应用)

Best Cow Line, Gold Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5063   Accepted: 1806 Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arrang

poj 3617 Best Cow Line(贪心)

 Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8579   Accepted: 2629 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges h

POJ 3617 Best Cow Line (贪心)

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11230   Accepted: 3329 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his