标签:class blog code 2014 string os
这题虽然简单但是挺不错的,因为过程很好,比较开发思维 和鼓励人,不像有些贪心太偏不好推反而让人厌烦
给出长度为N的字符串S,然后还有一个空串STR,每次有两个选择 1:删除S的头元素假加入STR中 2:删除S的尾元素加入STR中
是的STR字典序最小 并输出
开始可能没有什么顾虑的去想 每次比较S的头和尾元素 取小的那个删除并假如STR中,但是若S的头和尾元素一样的话这个方法就不行了,因为先取头或者尾还得看他们之间的元素,这时候是倒着来还是顺着好呢?那就直接拿顺的跟倒的进行字典序的大小比较就好了,这样当头尾相等时就能把他们中间的囊括进去,
做法:
字符串S,然后倒置得到S1,比较大小若S小,则取S的头部元素,若S大则取S的尾部元素,然后再把S倒置,再与它的倒置比较,如此循环的做N次即可
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype>
#define ll long long
#define LL __int64
#define eps 1e-8
#define inf 0xfffffff
//const LL INF = 1LL<<61;
using namespace std;
//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;
string s;
string str;
string ans;
string ch;
int main() {
int n;
bool flag = false;
while(cin>>n) {
while(n--) {
cin>>ch;
s += ch;
}
str = s;
reverse(s.begin(),s.end());
int len = s.length();
while(len--) {
if(str < s) {
ans += str[0];
str.erase(0,1);
}
else {
ans += str[str.length() - 1];
str.erase(str.length() - 1,1);
}
s = str;
reverse(s.begin(),s.end());
}
for(int i=0;i<ans.length();i++) {
cout<<ans[i];
if((i+1)%80 == 0)puts("");
}
puts("");
}
return 0;
}POJ3617 Best Cow Line 贪心,布布扣,bubuko.com
标签:class blog code 2014 string os
原文地址:http://blog.csdn.net/yitiaodacaidog/article/details/32947119