A. 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=s1s2sns=s1s2…sn is lexicographically smaller than string t=t1t2tmt=t1t2…tm if n<mn<m and s1=t1,s2=t2,,sn=tns1=t1,s2=t2,…,sn=tn or nmn≥m and there exists a number pp such that pmp≤m and s1=t1,s2=t2,,sp1=tp1s1=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 (2n21052≤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".

抱歉,可能是数据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;
}

 

 

 

posted @ 2018-11-12 23:37  black_hole6  阅读(337)  评论(3)    收藏  举报