1076A Minimizing the String

版权声明:大家一起学习,欢迎转载,转载请注明出处。若有问题,欢迎纠正! https://blog.csdn.net/memory_qianxiao/article/details/84024972

A. Minimizing the String

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string ss consisting of nn lowercase Latin letters.

You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.

String s=s1s2…sns=s1s2…sn is lexicographically smaller than string t=t1t2…tmt=t1t2…tm if n<mn<m and s1=t1,s2=t2,…,sn=tns1=t1,s2=t2,…,sn=tn or there exists a number pp such that p≤min(n,m)p≤min(n,m) and s1=t1,s2=t2,…,sp−1=tp−1s1=t1,s2=t2,…,sp−1=tp−1 and sp<tpsp<tp.

For example, "aaa" is smaller than "aaaa", "abb" is smaller than "abc", "pqr" is smaller than "z".

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of ss.

The second line of the input contains exactly nn lowercase Latin letters — the string ss.

Output

Print one string — the smallest possible lexicographically string that can be obtained by removing at most one character from the string ss.

Examples

input

Copy

3
aaa

output

Copy

aa

input

Copy

5
abcda

output

Copy

abca

Note

In the first example you can remove any character of ss to obtain the string "aa".

In the second example "abca" < "abcd" < "abcda" < "abda" < "acda" < "bcda".

题意:给出一个长度为n的字符串,最多删除一个字符,让整体字符串字典序最小。

题解:要让字符串字典序最小,一定是前面越小越好,所以从前面一个一个找,如果前面的字符大于后面的就是删除前面的

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    for(int i=0; i<n; i++)
        if(s[i]>s[i+1])
        {
            s.erase(i,1);///erase(i,1)删除第i位开始的1个字符
            break;
        }
    cout<<s<<endl;
    return 0;
}

python:

n=int(input())
s=input()
for i in range(n-1):
    if s[i]>s[i+1]:
        print(s[0:i]+s[i+1:n])
        exit()
print(s[0:n-1])
      

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/84024972
今日推荐