• A Magic Lamp(贪心+链表)


    A Magic Lamp

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


    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
     题解:给一个数,去掉一部分后得到的数最小,真是写醉了。。。各种wa,最后都想到了负数,思路没错,就是贪心,找到n[i]>n[j]就减去i这个数,最后过了,现在仍然感觉自己思路每错,换种写法就会ac。。。
    代码:
     1 #include<cstdio>
     2 #include<cstring>
     3 #define mem(x,y) memset(x,y,sizeof(x))
     4 const int MAXN=5050;
     5 char n[MAXN];
     6 int vis[MAXN];
     7 int main(){
     8     int m,t;
     9     while(~scanf("%s%d",n,&m)){
    10         mem(vis,0);
    11         t=strlen(n);
    12         int r=t-1;
    13         for(int i=0;i<m;i++){
    14             int cnt=0;
    15             for(int j=0;j<r;j++){
    16                 if(vis[j])continue;
    17                 int x=j+1;
    18                 while(vis[x])x++;//
    19                 if(n[j]>n[x]){
    20                     vis[j]=1;cnt=1;break;
    21                     /*比赛时候这样写的,一直wa仍然感觉没错。。。 
    22                     if(n[j]<n[j+1]){
    23                     cnt=1;
    24                     vis[j]=1;
    25                     n[j]=n[j+1];
    26                     break;
    27                 }
    28                     */
    29                 }
    30             }
    31             if(!cnt)vis[r--]=1;
    32         }int flog=1;
    33         for(int i=0;i<t;i++){
    34             if(vis[i])continue;
    35             if(flog&&n[i]=='0')continue;
    36             flog=0;printf("%c",n[i]);
    37         }
    38         if(flog)printf("0");
    39         puts("");
    40     }
    41     return 0;
    42 }

     链表:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 const int MAXN=1010;
     9 struct Node{
    10     int pre,next,val;
    11 };
    12 Node lis[MAXN];
    13 char s[MAXN];
    14 int main(){
    15     int n,len;
    16     while(~scanf("%s%d",s,&n)){
    17         mem(lis,0);
    18         len=strlen(s);
    19         for(int i=1;i<=len;i++){
    20             lis[i].pre=i-1;
    21             lis[i].val=s[i-1]-'0';
    22             lis[i].next=i+1;
    23         }
    24         lis[0].next=1;lis[len+1].pre=len;
    25         int p,q;
    26         while(n--){
    27             p=0;
    28             while(p!=len+1){
    29                 q=lis[p].next;
    30                 if(lis[p].val>lis[q].val){
    31                     lis[q].pre=lis[p].pre;
    32                     q=lis[p].pre;lis[q].next=lis[p].next;
    33                     break;
    34                 }
    35                 p=lis[p].next;
    36             }
    37         }
    38         p=0;
    39         while(lis[p].val==0&&p!=len+1)p=lis[p].next;
    40         if(p==len+1)puts("0");
    41         else{
    42             while(lis[p].next!=n+1){
    43                 printf("%d",lis[p].val);p=lis[p].next;
    44             }
    45             puts("");
    46         }
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    hibernate中持久化对象的生命周期(转载)
    IDEA调试技巧之条件断点
    POI中不推荐的方法与其替代的方法
    visualvm监控类是否是多例模式
    IDEA中Maven项目使用Junit4单元测试的写法
    JPQL的关联查询
    poi的cellstyle陷阱,样式覆盖
    studio 连不上远程仓库的各种原因分析
    Concurrent usage detected
    我的SSH框架实例(附源码)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4965196.html
Copyright © 2020-2023  润新知