A Magic Lamp(HDUOJ 3183)

来源:互联网 发布:田岛美工刀片价格 编辑:程序博客网 时间:2024/06/03 19:04

题目:

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1572    Accepted Submission(s): 611


Problem Description
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?
 

Input
There 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.
 

Output
For 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 1100001 212345 254321 2
 

Sample Output
1310123321
 

Source
HDU 2009-11 Programming Contest
 

Recommend
lcy


代码1:

/**********************************************************                                                        **     @name:HDUOJ 3183                                   **     @author:npufz                                      ***********************************************************//*#include <bits/stdc++.h>*/#include <iostream>#include <cstdlib>#include <cmath>#include <cstdio>using namespace std;int  dp[1005][10];char  s[1005];int MIN(int a,int b,int jb1,int jb2){    return a>b?jb2:jb1;}int rmq_st(int n){    for(int i=1;i<=n;i++) dp[i][0]=i-1;    int m=(int )(log(1.0*n)/log(2.0));    for(int j=1;j<=m;j++)    {       int t=n-(1<<j)+1;       for(int i=1;i<=t;i++)       {           dp[i][j]=MIN(s[dp[i][j-1]],s[dp[i+(1<<(j-1))][j-1]],dp[i][j-1],dp[i+(1<<(j-1))][j-1]);       }   }   return 0;}int rmq_find(int l,int r){    int k=(int ) (log(1.0*(r-l+1))/log(2.0));    return MIN(s[dp[l][k]],s[dp[r-(1<<k)+1][k]],dp[l][k],dp[r-(1<<k)+1][k]);}int main(){   int   m,num,left,right,p;   bool  flag;   while(~scanf("%s %d",s,&m))   {       num=0;flag=false;     while(s[num])num++;       rmq_st(num);       p=-1;       flag=false;    for(right=m+1;right<=num;right++)    {       left=p+2;       p=rmq_find(left,right);       if(flag==false)       {           if(s[p]=='0');           else flag=true;       }        if(flag) printf("%c",s[p]);   }   if(flag==false) printf("0");   printf("\n"); }return 0;}
反思:这个题目是一个贪心,直接暴力贪心就能过,因为数据规模很小,题目解决的关键就是从前往后就直接在(m+1)里找出最小的一个就是第一个数,因为划掉m个,那么剩下的就是(n-m)个,这(n-m)个必定在前(m+1)个当中有一个,找到最小的作为首位,然后就是从那一位的后一位起,重复上述操作就可以了,注意这里用了RMQ_ST求解,由于找的是脚标的关系,所以DP数组的值就该是脚标,一开始一直考虑脚标的问题,后来自己定义了一个MIN函数就搞定了

代码2:

/**********************************************************                                                        **     @name:HDUOJ 3813                                   **     @author:npufz                                      ***********************************************************//*#include <bits/stdc++.h>*/#include <iostream>#include <cstdlib>#include <cmath>#include <cstdio>using namespace std;int main(){   char  s[1005];   int  i,m,min1,num,left,right,p;   bool  flag;   while(~scanf("%s %d",s,&m))   {       num=0;flag=false;     while(s[num])num++;        p=-1;       for(right=m;right<num;right++)       {           left=p+1;min1='0'+10;           for(i=left;i<=right;i++)           {               if(s[i]<min1)               {                   p=i;                   min1=s[i];               }           }           if(flag==false)           {               if(s[p]=='0');               else                   flag=true;           }           if(flag)            printf("%c",s[p]);        }        if(flag==false) printf("0");        printf("\n"); }return 0;}

反思:直接暴力,不解释



0 0
原创粉丝点击