贪心算法-Best cow line-字典序问题
代码:
#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
char input[2005];
int main()
{
int n;
int i,j;
while(scanf("%d",&n)!=EOF)
{
int lbit=0,rbit=n-1;//左边的位:lbit 右边的位:rbit
getchar();
for(i=0;i<=n-1;i++)
scanf("%c",&input[i]);
getchar();
while(lbit<=rbit)
{
bool cmpleft=false;//定义字典序比较的bool变量 如果左边小为true 右边小为false
//左起字符串与右起字符串比较
for(i=0;lbit+i<rbit;i++)
{
if(input[lbit+i]<input[rbit-i])
{
cmpleft=true;
break;
}
else if(input[lbit+i]>input[rbit-i])
{
cmpleft=false;
break;
}
//如果相等continue
}
if(cmpleft)putchar(input[lbit++]);//从左边取
else putchar(input[rbit--]);//从右边取
}
printf("\n");
}
return 0;
}
To improve is to change, to be perfect is to change often.