1076A Minimizing the String
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])