zoukankan      html  css  js  c++  java
  • A Magic Lamp(HDU

    Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams. 
    The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum. 
    You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream? 

    InputThere are several test cases. 
    Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero. 
    OutputFor each case, output the minimum result you can get in one line. 
    If the result contains leading zero, ignore it. 
    Sample Input

    178543 4 
    1000001 1
    100001 2
    12345 2
    54321 2

    Sample Output

    13
    1
    0
    123
    321

    题意,从一个长度为N的字符串中删除M个字符,使剩下的字符串最小。

    从N长字符串中删除M个字符,相当于从N长字符串中挑N-M个字符组成最小字符串,那么第一个字符应该是从[0,M]中找最小字符,假如它的下标为K,则第二个寻找范围为[k+1,M+1],
    依此类推,直到找完,用ST表模拟即可。

    #include<iostream>
    #include<cstdio>
    #include<sstream>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn = 1001;
    int d[maxn][10];
    char a[maxn],num[maxn];
    int n, m;
    int minn(int i,int j)//核心
    {
        return a[i]<=a[j]?i:j;
    }
    int getminn(int l,int r)
    {
        int k=log2(r-l+1);
        return minn(d[l][k],d[r-(1<<k)+1][k]);
    }
    int main()
    {
        while(scanf("%s%d",a,&m)!=EOF)
        {
            int len=strlen(a);
            n=len;
            m=len-m;
            for(int i=1;i<n;i++)  //下标的序列
                d[i][0]=i;
            for(int j=1;(1<<j)<= n;j++)
                for(int i=0;i+(1<<j)-1< n;i++)
                    d[i][j]=minn(d[i][j-1], d[i+(1<<(j-1))][j-1]);//返回代表值小的下标值. 
            int i,j;
            i=j=0;
            while(m--)
            {
                //int l=i;
                //int r=len-m-1;
                 i=getminn(i,len-m-1);  //while中m已经--了所以例如第一次就相当于0到m去找第一位
                   //int k=log2(r-l+1);
                   //i==minn(d[l][k],d[r-(1<<k)+1][k]);
                num[j++]=a[i++];
            }
            for(i=0;i<j;i++)
            if(num[i]!='0')
            break;
            if(i==j)
            {
                printf("0
    ");
                continue;
            }
            while(i<j)
            {
                printf("%c",num[i]);
                i++;
            }
            printf("
    ");
        }
    }
    View Code
    
    
    
     
  • 相关阅读:
    电脑U盘启动制作
    windows系统使用
    CentOS升级Openssl至openssl-1.1.0
    PHP编译安装时常见错误解决办法
    阿里 Linux服务器外网无法连接MySQL解决方法
    centos 下 sphinx安装和配置
    集成百度编辑器 ueditor 后端配置项没有正常加载,上传插件不能正常使用!
    nginx 环境 thinkphp 隐藏index.php
    在 Linux 下搭建 Git 服务器
    MySQL远程连接不上的解决方法
  • 原文地址:https://www.cnblogs.com/switch-waht/p/11405550.html
Copyright © 2011-2022 走看看