zoukankan      html  css  js  c++  java
  • hdu 3183 A Magic Lamp

    传送门

    A Magic Lamp

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


    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 1 100001 2 12345 2 54321 2
     
    Sample Output
    13 1 0 123 321
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3188 3189 3184 3185 3186 

    题意:

    题意:给出一个不超过1000位的数,求删去m个数字以后形成的最小的数是多少。

    题解:

    贪心,前面的数要小于后面的数,用栈维护

    note:

    注意前导0的去除

    16573675 2016-03-16 19:08:20 Accepted 3183 15MS 1732K 1882B C++ czy

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <stack>
     6 #include <cctype>
     7 #include <vector>
     8 #include <cmath>
     9 #include <map>
    10 #include <queue>
    11 
    12 #define ll long long
    13 #define eps 1e-8
    14 #define N 1004
    15 #define inf 0x3ffffffffffffff
    16 
    17 using namespace std;
    18 
    19 char s[N];
    20 int m;
    21 int l;
    22 
    23 void solve()
    24 {
    25     stack<char> st;
    26     stack<char> ans;
    27     int i;
    28     for(i = 0;i < l;i++){
    29         while(!st.empty() && m > 0 && st.top()>s[i]){
    30             m--;
    31             st.pop();
    32         }
    33         if(m == 0) break;
    34         st.push(s[i]);
    35     }
    36     while(!st.empty() && m > 0){
    37         m--;
    38         st.pop();
    39     }
    40     //printf(" i=%d l =%d
    ",i,l);
    41     int flag = 0;
    42     char te;
    43     while(!st.empty())
    44     {
    45         te = st.top();
    46         ans.push(te);
    47         st.pop();
    48         if(te != '0'){
    49             flag = 1;
    50         }
    51     }
    52     if(flag == 0){
    53         for(;i<l;i++){
    54             if(s[i] != '0') break;
    55         }
    56         if(i == l) printf("0");
    57         for(;i<l;i++){
    58             printf("%c",s[i]);
    59         }
    60     }
    61     else{
    62         int ff = 0;
    63         while(!ans.empty()){
    64             te = ans.top();
    65             if(ff == 0){
    66                 if(te == '0'){
    67                     ans.pop();
    68                 }
    69                 else{
    70                     ff = 1;
    71                     printf("%c",te);
    72                     ans.pop();
    73                 }
    74             }
    75             else{
    76                 printf("%c",te);
    77                 ans.pop();
    78             }
    79 
    80         }
    81         for(;i<l;i++){
    82             printf("%c",s[i]);
    83         }
    84     }
    85     printf("
    ");
    86 }
    87 
    88 int main()
    89 {
    90     //freopen("in.txt","r",stdin);
    91     //scanf("%d",&T);
    92     //for(int ccnt=1;ccnt<=T;ccnt++){
    93     while(scanf("%s%d",s,&m)!=EOF){
    94         l = strlen(s);
    95         solve();
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    codeforces 57C 思维
    FOJ 2232 匈牙利算法找二分图最大匹配
    UVA 101 vector
    POJ 3070 矩阵mob
    codeforces 60B bfs
    codeforces 54A
    codeforces 466C 计数 codeforces 483B 二分 容斥
    FOJ 2213 简单几何
    CCF-最优配餐(BFS)
    杂论-FTP
  • 原文地址:https://www.cnblogs.com/njczy2010/p/5284758.html
Copyright © 2011-2022 走看看