zoukankan      html  css  js  c++  java
  • 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]='';
    }
    
    int main()
    {
        shuru();
       // printf("%s
    ",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
    */
    
  • 相关阅读:
    mac下配置openCV
    K最短路 A*算法
    KMP算法
    北航复试机试题
    1385重建二叉树
    二维数组中的查找
    简单的单向链表
    Getting Started with WebRTC [note]
    我的c漏洞
    PeerConnection
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934375.html
Copyright © 2011-2022 走看看