文章

19

粉丝

0

获赞

148

访问

4.9k

头像
后缀子串排序 题解:
P1294 上海交通大学机试题
发布于2025年3月10日 21:31
阅读数 249

显然解题分两步:1,获取子串。2,字典排序。

字典排序很简单,sort函数自然对字符串进行字典排序。而获取子串可以用c++自带的substr函数,具体用法不清楚的可以查一下CSDN或者Deepseek都可以,那么就基本解决了题目,只需要用一个vector存储截取下来的字符串就可以了。

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin >> s;
    vector<string> a;
    int ls = s.length();
    
    for(int i = 0;i < ls;i++){
        a.push_back(s.substr(i));
        }
    sort(a.begin(),a.end());
    for(int i = 0;i < ls;i++){
        cout << a[i];
        if(i != ls-1) cout << endl;
        }
    return 0;
}

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发