How B???!!!

How B😫

我尝试过这个,个人认为比较接近答案的贪心了,但是为什么会wa啊

string f(const string& s)
{
    int n=s.size();
    if(n<=1)return "";
    int pos=n-1;
    for(int i=0; i<n-1; i++)if(s[i]>s[i+1])
    {
        pos=i;
        break;
    }
    return s.substr(0, pos)+s.substr(pos+1);
}
 
bool cmp(const string &x, const string &y) 
{
    return x+y<y+x;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n; cin >> n;
    vector<string> b;
    for(int i=0; i<n; i++)
    {
        string a; cin >> a;
        // pnt(f(a));
        b.push_back(f(a));
    }
    sort(b.begin(),b.end(),cmp);
    // cout << b << endl;
    string ans;
    for(auto &x:b)ans+=x;
    cout << ans;
 
 
    return 0;
}

全部评论
return x+y<y+x;这个问题吧。 改成这样:  if (x+ y != x + y) return x + y < x + y;         return x < y;
1 回复 分享
发布于 10-31 21:45 湖南
空串不能加到排序里,我就这样一直wa,对拍才找出来
点赞 回复 分享
发布于 10-31 21:55 广东
此贴终结,没考虑到加""空字符串导致字符串排序时,会有错误的偏序关系,存入ans时,加个if(a.size()<=1)特判即可
点赞 回复 分享
发布于 10-31 21:54 浙江

相关推荐

10-29 18:20
济南大学 Java
用微笑面对困难:他不是人事吗,怎么净特么不干人事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务