标签:
http://acm.hdu.edu.cn/showproblem.php?pid=3183
问题等价与取N-M个数,每次取的时候保证后面能取的个数足够,并且取的数最小 查询最小用rmq
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 6 using namespace std; 7 8 9 const int maxn = 10000 + 5; 10 11 int N,M,d[maxn][20]; 12 char s[maxn]; 13 14 15 struct RMQ{ 16 void init() 17 { 18 N = strlen(s); 19 for (int i = 0;i<N;i++) 20 d[i][0] = s[i]; 21 for (int k = 1;(1<<k)<=N;k++) 22 { 23 for (int i = 0;i<N;i++) 24 d[i][k] = min(d[i][k-1],d[i+(1<<(k-1))][k-1]); 25 26 } 27 } 28 int query(int L,int R) 29 { 30 int k = 0; 31 while (1<<(k+1)<=R-L+1) k++; 32 return min(d[L][k],d[R-(1<<k)+1][k]); 33 } 34 }; 35 36 RMQ rmq; 37 38 int main() 39 { 40 while (~scanf("%s%d",s,&M)) 41 { 42 rmq.init(); 43 M = N - M; 44 int mv = 0; 45 bool flag = true; 46 for (int i = M;i;i--) 47 { 48 int c = rmq.query(mv,N-i); 49 while (mv<N&&s[mv]!=c) mv++; 50 mv++; 51 if (c==‘0‘&&flag) 52 continue; 53 flag = false; 54 printf("%c",c); 55 56 } 57 if (flag) 58 printf("0"); 59 printf("\n"); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/yifi/p/4551615.html