poj3617【贪心】

题意:

给定长度为N的字符串S,要构造一个长度为N的字符串T串。
从S的头部删除一个字符,加到T的尾部
从S的尾部删除一个字符,加到T的尾部
目标是构造字典序尽可能小的字符串。

思路:
贪心,每次取小,如果有相同,就往里面搜一下,)注意……输出…….pe……
code…..

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int Mod=1e9;
const int INF=0x3f3f3f3f;

const int N=2e3+10;
char a[N][3];
char s1[N];
char ans[N];
int n;

void shuru()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%s",a[i]);
    for(int i=0;i<n;i++)
        s1[i]=a[i][0];
    s1[n]='\0';
}

int main()
{
    shuru();
   // printf("%s\n",s1);
    int s,t,num;
    s=0;
    t=n-1;
    num=0;
    while(s<=t)
    {
        if(s1[s]==s1[t])
        {
            printf("%c",s1[s]);
            int i,j;
            i=s;
            j=t;
            while(s1[i]==s1[j])
            {
                i++;
                j--;
            }
            if(s1[i]>s1[j])
                t--;
            else
                s++;
        }
        else
        {
            if(s1[s]>s1[t])
            {
                printf("%c",s1[t]);
                t--;
            }
            else
            {
                printf("%c",s1[s]);
                s++;
            }
        }
        num++;
        if(num==80)
        {
            num=0;
            puts("");
        }
    }
}
/*
6
A
C
D
B
C
A
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值