zoukankan      html  css  js  c++  java
  • [贪心] COJ 1236 删数游戏

    每次查找第一个降序的首字符,如果不存在就删除结尾字符,链表,O(n)。

    # include <cstdio>
    # include <iostream>
    
    using namespace std;
    
    struct node
    {
        char ch;
        node *pre, *next;
    };
    
    void b(node *head, char *s)
    {
        head->ch = s[0];
        node *tmp = head;
        for (int i = 0; s[i]; ++i)
        {
            node *cur = new node;
            cur->ch = s[i+1];
            tmp->next = cur;
            cur->pre  = tmp;
            cur->next = NULL;
            tmp = cur;
        }
    }
    
    node* d(node *head, int n)
    {
        node *tmp = head;
        while (n)
        {
            if ((tmp->ch) > (tmp->next->ch))
            {
                --n;
                if (tmp == head)
                {
                    head = head->next;
                    head->pre = NULL;
                    delete tmp;
                    tmp = head;
                }
                else
                {
                    node *cur = tmp->pre;
                    tmp->pre->next = tmp->next;
                    tmp->next->pre = tmp->pre;
                    delete tmp;
                    tmp = cur;
                }
            }
            else tmp = tmp->next;
        }
        return head;
    }
    
    void p(node *head)
    {
        node *cur = head;
        while (cur->ch == '0') cur = cur->next;
        if (cur->ch == '\0') puts("0");
        else
        {
            while (cur->ch != '\0')
            {
                putchar(cur->ch);
                cur = cur->next;
            }
            putchar('\n');
        }
    }
    
    void k(node *p)
    {
        if (p->next)
        {
            k(p->next);
            delete p;
        }
    }
    
    int ss;
    char nn[2005];
    
    void solve(void)
    {
        node *head = new node;
        head->pre = NULL;
        head->next = NULL;
        b(head, nn);
        head = d(head, ss);
        p(head);
        k(head);
    }
    
    int main()
    {    
        while (~scanf("%s%d", nn, &ss))
            solve();
        
        return 0;
    }
  • 相关阅读:
    sklearn linear_model,svm,tree,naive bayes,ensemble
    便利过滤
    js下载
    小程序修改radio的大小
    el-tree 问题与需求
    jsp页面用html引入vue.js注意问题
    WebPack
    yarn
    vue-cli 4以上 vue.config.js
    Cannot find module 'core-js/modules/es6.regexp.constructor'
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2636620.html
Copyright © 2011-2022 走看看