hdu 3183 A Magic Lamp ( 暴力)

hdu3183题目链接

题意:n位长的数字串(n<=1000),删掉m个(m<=n),使得剩下的数字串表示的数字最小。 忽略前导0.

思路:暴力搞就可以。要注意每位数字是有一定位置的范围的。比如当前是第i位数字,后面还要取n-m-i位数字,那么第i位数字最多只能取到第k位,k=m+i,因为这样才能保证后面还有n-m-i位数字。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年05月16日 星期一 13时15分44秒
 4File Name :code/hdu/3183.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E3+7;
34int n,m;
35
36int ans[N];
37char st[N];
38int main()
39{
40	#ifndef  ONLINE_JUDGE 
41	freopen("code/in.txt","r",stdin);
42  #endif
43
44	while (~scanf("%s %d",st,&m))
45	{
46	    n = strlen(st);
47	    ms(ans,0);
48
49	    int x = 0 ;
50	    for ( int i = 1 ; i <= n-m ; i++)
51	    {
52		int MIN = inf;
53		for ( int j = x+1 ; j <= m+i ; j++)
54		{
55		    int val = st[j-1]-'0';
56		    if (val<MIN)
57		    {
58			MIN = val;
59			x = j;
60		    }
61		}
62		ans[i] = MIN;
63	    }
64
65
66	    bool zero = true;
67	    bool output = false;
68	    for ( int i = 1; i <= n-m ; i++)
69	    {
70		if (ans[i]!=0) zero = false;
71		if (!zero)
72		{
73		    output = true;
74		    printf("%d",ans[i]);
75		}
76	    }
77	    if (output)
78	    printf("\n");
79	    else printf("0\n");
80
81	}
82
83
84
85  #ifndef ONLINE_JUDGE  
86  fclose(stdin);
87  #endif
88    return 0;
89}