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。。。
代码:
 #include<cstdio>
#include<cstring>
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
char n[MAXN];
int vis[MAXN];
int main(){
int m,t;
while(~scanf("%s%d",n,&m)){
mem(vis,);
t=strlen(n);
int r=t-;
for(int i=;i<m;i++){
int cnt=;
for(int j=;j<r;j++){
if(vis[j])continue;
int x=j+;
while(vis[x])x++;//
if(n[j]>n[x]){
vis[j]=;cnt=;break;
/*比赛时候这样写的,一直wa仍然感觉没错。。。
if(n[j]<n[j+1]){
cnt=1;
vis[j]=1;
n[j]=n[j+1];
break;
}
*/
}
}
if(!cnt)vis[r--]=;
}int flog=;
for(int i=;i<t;i++){
if(vis[i])continue;
if(flog&&n[i]=='')continue;
flog=;printf("%c",n[i]);
}
if(flog)printf("");
puts("");
}
return ;
}

链表:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
struct Node{
int pre,next,val;
};
Node lis[MAXN];
char s[MAXN];
int main(){
int n,len;
while(~scanf("%s%d",s,&n)){
mem(lis,);
len=strlen(s);
for(int i=;i<=len;i++){
lis[i].pre=i-;
lis[i].val=s[i-]-'';
lis[i].next=i+;
}
lis[].next=;lis[len+].pre=len;
int p,q;
while(n--){
p=;
while(p!=len+){
q=lis[p].next;
if(lis[p].val>lis[q].val){
lis[q].pre=lis[p].pre;
q=lis[p].pre;lis[q].next=lis[p].next;
break;
}
p=lis[p].next;
}
}
p=;
while(lis[p].val==&&p!=len+)p=lis[p].next;
if(p==len+)puts("");
else{
while(lis[p].next!=n+){
printf("%d",lis[p].val);p=lis[p].next;
}
puts("");
}
}
return ;
}
上一篇:【浅谈web安全】大企业安全:从员工下手


下一篇:剑指OFFER之矩形覆盖(九度OJ1390)