BUAA 169 电话费
http://oj55.bianchengla.com/problem/169/
还有这个,不怎么好做,最后用栈做出来了。
感觉比较有用的一个数据是:
10 3
1010101010
代码如下:
1 #include <cstdio> 2 #include <iostream> 3 4 using namespace std; 5 6 const int N = 11111; 7 int n, k; 8 char s[N], stk[N], mx[N]; 9 10 int main() { 11 while (~scanf("%d%d%s", &n, &k, s)) { 12 int top = -1; 13 if (k == n - 1) { 14 char mn = '9'; 15 for (int i = 0; i < n; i++) mn = min(s[i], mn); 16 putchar(mn); puts(""); 17 continue; 18 } 19 for (int i = 0; i < n; i++) { 20 if (s[i] != '0') while (~top && k >= top + 1 && mx[top] > s[i]) top--, k--; 21 while (k && ~top && stk[top] > s[i]) { 22 if (s[i] == '0' && top == 0) break; 23 top--; k--; 24 } 25 stk[++top] = s[i]; 26 if (top > 0) mx[top] = max(s[i], mx[top - 1]); 27 else mx[top] = s[i]; 28 } 29 while (k--) top--; 30 //printf("top %d\n", top); 31 for (int i = 0; i <= top; i++) putchar(stk[i]); puts(""); 32 } 33 return 0; 34 }
——written by Lyon