题目链接:http://poj.org/problem?id=3617
大概的中文意思:
比较好的一种思路,令输入的序列为s1,s1的反转的序列为s2;
按照字符串的比较来比较s1和s2的大小,若s1!=s2 则取小的那一个的首字母加入到T序列
若s1 == s2 则默认取s1的首字母加入到T序列。取谁的首字母,谁就往后移动一步。
#include <iostream>
#include <algorithm>
using namespace std;
char str[2001];
void fun(const int count1);
bool cmp(int count1,int L,int R);
int main(){
int count1;
cin>>count1;
for(int i = 0;i<count1;i++){
scanf(" %c",&str[i]);
}
fun(count1);
return 0;
}
bool cmp(int count1,int L,int R){ //字符串比较
for(;L<=R;L++,R--){
if(str[L]!=str[R])
return str[L]<str[R];
}
return true;
}
void fun(const int count1){ //贪心
int L = 0,R = count1-1,countchar = 0;
while(L<=R){
if(cmp(count1,L,R)){
printf("%c",str[L]);
L++;
} else{
printf("%c",str[R]);
R--;
}
if(++countchar%80 == 0)
putchar('\n');
}
}