HDU 3183 Magic Lamp(單調棧)
阿新 • • 發佈:2020-08-27
題目大意
給一串數字要求刪除幾個數字之後輸出最少的數字。
解題思路
很明顯假如前一位比後一位大的話,刪除前一位結果更優,如果剩下的是個不下降序列還能刪的話就從後往前刪,用單調棧就能很簡單的解決。
程式碼
const int maxn = 1e6+10; const int maxm = 2e2+10; char str[maxn]; int k; stack<char> sk; string s; int main() { IOS; while(cin >> str >> k) { int len = strlen(str); if(len==k) { cout << "0" << endl; continue; } for (int i = 0; str[i]; ++i) { while(k && !sk.empty() && sk.top()>str[i]) --k, sk.pop(); sk.push(str[i]); } while(k--) sk.pop(); while(!sk.empty()) s += sk.top(), sk.pop(); while(s.size()!=1 && s.back()=='0') s.pop_back(); reverse(s.begin(), s.end()); cout << s << endl; s.clear(); } return 0; }