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
既然要删去m个数之后要求的数最小。那么肯定保留n-m个数字。那么每次在区间[i,i+m]查询最小值即可。
但是有个坑点,虽然这道题没有,但是底下的那道有,就是比较的时候,如果值相等,要选位置靠前的。
这也是贪心的思想。
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int INF = 0x3f3f3f3f; const int maxn=1100; int dp[maxn][20]; int num[maxn]; char str[maxn],ans[maxn]; int n,m; int MIN(int a,int b) { if(num[a]<=num[b]) return a; return b; } void init() { for(int i=1;i<=n;i++) dp[i][0]=i; for(int j=1;(1<<j)<=n;j++) for(int i=1;i+(1<<j)-1<=n;i++) dp[i][j]=MIN(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } int RMQ(int l,int r) { int k=(int)(log(r-l+1)/log(2.0)); return MIN(dp[l][k],dp[r-(1<<k)+1][k]); } int main() { while(~scanf("%s%d",str+1,&m)) { n=strlen(str+1); REPF(i,1,n) num[i]=str[i]-'0'; init();CLEAR(ans,0); int l=1,r=1+m; int cnt=0,pos; while(cnt<n-m) { pos=RMQ(l,r); ans[cnt++]=str[pos]; l=pos+1; r++; } int i; for(i=0;i<cnt;i++) if(ans[i]!='0') break; if(i==cnt) { puts("0"); continue; } for(;i<cnt;i++) printf("%c",ans[i]); puts(""); } return 0; }
NBU 1449:
Description
在给定的n个数字的数字串中,删除其中k(k< n)个数字后,剩下的数字按原次序组成一个新的正整数。请确定删除方案,使得剩下的数字组成的新正整数最大。
Input
输入一个由n个数字组成的正整数(1< n<=100000),再输入一个整数k(0<=k<n),表示要删除k位数字。 < n),输入的数字保证没有前导0。<n<=100000),再输入一个整数k(0<=k<n),表示要删除k位数字。
Output
输出删除k位后的最大整数
Sample Input
102 1
Sample Output
12
求删掉之后最大值,同样的方法。
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int INF = 0x3f3f3f3f; const int maxn=1e5+10; int dp[maxn][20]; int num[maxn]; char str[maxn],ans[maxn]; int n,m; int MAX(int a,int b) { if(num[a]>=num[b]) return a; return b; } void init() { for(int i=1;i<=n;i++) dp[i][0]=i; for(int j=1;(1<<j)<=n;j++) for(int i=1;i+(1<<j)-1<=n;i++) dp[i][j]=MAX(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } int RMQ(int l,int r) { int k=(int)(log(r-l+1)/log(2.0)); return MAX(dp[l][k],dp[r-(1<<k)+1][k]); } int main() { while(~scanf("%s%d",str+1,&m)) { n=strlen(str+1); REPF(i,1,n) num[i]=str[i]-'0'; init();CLEAR(ans,0); int l=1,r=1+m; int cnt=0,pos; while(cnt<n-m) { pos=RMQ(l,r); ans[cnt++]=str[pos]; l=pos+1; r++; } int i; for(i=0;i<cnt;i++) if(ans[i]!='0') break; if(i==cnt) { puts("0"); continue; } for(;i<cnt;i++) printf("%c",ans[i]); puts(""); } return 0; }