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
    
    
    
     
  • 相关阅读:
    docker固定IP地址重启不变
    关于 CentOS 7 里面 普通用户 Ulimit max user processes 值的问题
    CentOS tengine mysql 5.7 php 5.6
    strace 分析 跟踪 进程错误
    WPF中的字体改善
    C#数据类型-string
    WPF中的文字修饰
    Winform与WPF对话框(MessageBox, Dialog)之比较
    WPF中将四个数字字符串值(比如:"10,10,300,300")转为Rect
    WPF中嵌入Flash(ActiveX)
  • 原文地址:https://www.cnblogs.com/switch-waht/p/11405550.html
Copyright © 2011-2022 走看看