A. Minimizing the String
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 n≥mn≥m and there exists a number pp such that p≤mp≤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".
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 .
Print one string — the smallest possible lexicographically string that can be obtained by removing at most one character from the string ss .
3 aaa
aa
5 abcda
abca
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".
抱歉,可能是数据cf数据改了,这是最初过了的,有漏洞,被大佬指出了,在vj也过不了了
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int main() { int n; cin>>n; string s; cin>>s; int k; for(int t=0;t<n-1;t++) { if(s[t]>s[t+1]) { k=t; break; } } for(int t=0;t<n;t++) { if(t==k) { continue; } cout<<s[t]; } cout<<endl; return 0; }
需要将k初始化
现在AC的代码
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int main() { int n; cin>>n; string s; cin>>s; int k=s.length()-1; for(int t=0;t<n-1;t++) { if(s[t]>s[t+1]) { k=t; break; } } for(int t=0;t<n;t++) { if(t==k) { continue; } cout<<s[t]; } cout<<endl; return 0; }
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!

浙公网安备 33010602011771号