Codeforces 1076A - Minimizing the String

题解链接

https://lucien.ink


题目链接

http://codeforces.com/contest/1076/problem/A


题意

你有一个字符串,你最多可以删掉一个字母,使得剩下的字符串的字典序最小。


思路

从左向右找到第一个当前字母小于下一个字母的位置,删掉这个字母即可。


实现

https://pasteme.cn/2739

#include <bits/stdc++.h>
const int maxn = int(2e5) + 7;
char str[maxn];
int n, ans = -1;
int main() {
    scanf("%d %s", &n, str + 1);
    for (int i = 1; i <= n && ans == -1; i++) if (str[i] > str[i + 1]) ans = i;
    for (int i = 1; i <= n; i++) if (i != ans) printf("%c", str[i]);
    return 0;
}
最后修改:2018 年 12 月 22 日
谢谢老板!