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
    
    
    
     
  • 相关阅读:
    python深浅拷贝
    软件开发目录规范
    编码规范
    python进程、线程、协程的介绍及使用
    soket粘包问题及解决方案
    python socket通信
    数据开发_机器学习
    数据开发_开发工具以及工具链
    数据开发_Python读取文件
    数据开发_Java设计模式_IO以及读取资源文件
  • 原文地址:https://www.cnblogs.com/switch-waht/p/11405550.html
Copyright © 2011-2022 走看看