3069 Best Cow Line(贪心算法)

来源:互联网 发布:网页在线人数统计php 编辑:程序博客网 时间:2024/06/06 06:34

牛的编号

贪心算法,按照字典序比较S和反转后的S2;如果s小则从s头取出一个文字输入到 t中;如果s2小,则从s2中取,相等无所谓


注意的是输出格式问题

Output

The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.

超过八十头牛就换行!一直格式错误

#include <iostream>#include<cstdio>using namespace std;int n;char s[2005];char t[2005];void solve(){    int num=0;    int a=0,b=n-1;    while(a<=b)    {        bool left=false;        for(int i=0;i+a<=b;i++)        {            if(s[i+a]<s[b-i])            {                left=true;                break;            }            else if(s[i+a]>s[b-i])            {                left=false;                break;            }        }        num++;        if(left) putchar(s[a++]);        else putchar(s[b--]);      if((num)%80==0)//控制输出格式            printf("\n");    }  cout<<endl;}int main(){    cin>>n;    for(int i=0;i<n;i++)        cin>>s[i];        solve();    return 0;}


0 0
原创粉丝点击