每次查找第一个降序的首字符,如果不存在就删除结尾字符,链表,O(n)。
# include# include using namespace std;struct node{ char ch; node *pre, *next;};void b(node *head, char *s){ head->ch = s[0]; node *tmp = head; for (int i = 0; s[i]; ++i) { node *cur = new node; cur->ch = s[i+1]; tmp->next = cur; cur->pre = tmp; cur->next = NULL; tmp = cur; }}node* d(node *head, int n){ node *tmp = head; while (n) { if ((tmp->ch) > (tmp->next->ch)) { --n; if (tmp == head) { head = head->next; head->pre = NULL; delete tmp; tmp = head; } else { node *cur = tmp->pre; tmp->pre->next = tmp->next; tmp->next->pre = tmp->pre; delete tmp; tmp = cur; } } else tmp = tmp->next; } return head;}void p(node *head){ node *cur = head; while (cur->ch == '0') cur = cur->next; if (cur->ch == '\0') puts("0"); else { while (cur->ch != '\0') { putchar(cur->ch); cur = cur->next; } putchar('\n'); }}void k(node *p){ if (p->next) { k(p->next); delete p; }}int ss;char nn[2005];void solve(void){ node *head = new node; head->pre = NULL; head->next = NULL; b(head, nn); head = d(head, ss); p(head); k(head);}int main(){ while (~scanf("%s%d", nn, &ss)) solve(); return 0;}