zoukankan      html  css  js  c++  java
  • poj 3617 弱鸡贪心

    比赛的时候扣了一道贪心的题目,不会写,,现在补一补一些基础的贪心。

    题意:给定一个字符串s,要求按下列操作生成一个新串t--每次从s串中的最前和最后取一个字符给t,要求生成的t字典序最小。

    题解:由于是要求字典序最小,满足局部最优的思想贪心的取就可以了(这里要注意的是相等的情况)

    ac代码:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    int main()
    {
        int n;
        scanf("%d",&n);
        string s="";
        while(n--)
        {
            string ss;
            cin>>ss;
            s+=ss;
        }
        string t="";
        int i=0;
        int j,len;
        len=s.length();
        j=len-1;
        while(i<=j)
        {
            if(s[i] < s[j])
            {
                t+=s[i];
                i++;
            }
            else if(s[i] > s[j])
            {
                t+=s[j];
                j--;
            }
            else
            {
               int ti=i;
               int tj=j;
               while(s[ti]==s[tj] && ti<=tj)
               {
                   ti++;
                   tj--;
               }
               if(ti > tj)
               {
                   for(int z=i;z<=j;z++) t+=s[i];
                   break;
               }
               if(s[ti] < s[tj])
               {
                   t+=s[i];
                   i++;
               }
               else
               {
                   t+=s[j];
                   j--;
               }
    
            }
        }
        int tlen=t.length();
        int ret=0;
        for(int i=0;i<tlen;i++)
        {
            ret++;
            cout<<t[i];
            if(ret==80)
            {
                ret=0;
                cout<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    计算机硬件基础
    元类
    内置函数
    单例模式的三种实现方式
    字符编码
    odoo权限
    odoo api介绍
    odoo 二次开发小记不定时更新
    js与jQuery区别
    Cookie, LocalStorage 与 SessionStorage说明
  • 原文地址:https://www.cnblogs.com/z1141000271/p/7424805.html
Copyright © 2011-2022 走看看