1. 程式人生 > >Day2-T1

Day2-T1

原題目

  Describe:貪心,左邊和右邊中選字典序小的

  code:

#include<bits/stdc++.h>
using namespace std;
int n,step,head,tail;
char p[50005];
int main()
{
    freopen("bcl.in","r",stdin);
    freopen("bcl.out","w",stdout);
    cin>>n;head=1,tail=n;
    for(int i=1;i<=n;i++)cin>>p[i];
    
while(head<tail) { if(p[head]<p[tail])putchar(p[head]),head++; else if(p[head]>p[tail])putchar(p[tail]),tail--; else{ int i=head,j=tail; while(i<j&&p[i]==p[j])i++,j--; if(p[i]<p[j])putchar(p[head]),head++;
else putchar(p[tail]),tail--; } step++,step%=80; if(!step)puts(""); } putchar(p[head]); return 0; }