poj3617 Best Cow Line【贪心】

74 篇文章 0 订阅
48 篇文章 0 订阅

题目链接:http://poj.org/problem?id=3617
题意:给你一个长度为n的字符串S,让你用这个字符串构建一个新的字符串T,构建过程可以反复使用一下任意一种操作
将S的第一个元素删除,放在T的尾部
将S的最后一个元素删除,放在T的头部
让你尽可能的使得T的字典序最小
解析:每次从S拿元素时,只要保证拿到的最小即可,如果头尾选择的元素相等则比较他们下一位,直至比出大小为止
注意一个坑点,就是输出每行只能80个字符,不然会PE

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 2005;
char a[maxn];
int main(void)
{
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++)
    {
        scanf("%c",&a[i]);
        getchar();
    }
    int l=0,r = n-1;
    int cnt = 0;
    while(l<=r)
    {
        int t1=l,t2=r;
        bool lb=false;
        while(a[t1]==a[t2])
        {
            if(a[t1+1]<a[t2-1])
            {
                lb = true;
                break;
            }
            else
            {
                t1++;
                t2--;
            }
        }
        if(lb || a[l]<a[r])
        {
            printf("%c",a[l]);
            l++;
        }
        else
        {
            printf("%c",a[r]);
            r--;
        }
        cnt++;
        if(cnt==80)
        {
            puts("");
            cnt = 0;
        }
    }
    if(cnt!=0)
        puts("");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值