九度oj 1099 后缀子串排序

题目描述:

对于一个字符串,将其后缀子串进行排序,例如grain
其子串有:
grain 
rain 
ain 
in 
n

然后对各子串按字典顺序排序,即: 
ain,grain,in,n,rain

输入:

每个案例为一行字符串。

输出:

将子串排序输出

样例输入:
grain
样例输出:
ain
grain
in
n
rain
来源:

2010年上海交通大学计算机研究生机试真题


题目:

如上

注意点:

使用sort比较两个字符串的字典序的时候,记得要检查边界,先确定游标指向的位置可以访问,然后再访问


Code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>


bool cmp(std::string a, std::string b) {
int count = 0;
int size = a.size() > b.size() ? b.size() : a.size();
while (count < size && a[count] == b[count]) {
count++;
}
int a_num, b_num;
if (a[count] - 'a' >= 0 && a[count] - 'a' <= 26) {
a_num = a[count] - 'a';
}
else {
a_num = a[count] - 'A';
}
if (b[count] - 'a' >= 0 && b[count] - 'a' <= 26) {
b_num = b[count] - 'a';
}
else {
b_num = b[count] - 'A';
}
return a_num < b_num;
}


int main() {
std::string input;
while (std::cin >> input) {
std::vector<std::string> input_vector;
for (int i = input.size() - 1; i >= 0; i--) {
std::string temp = input.substr(i, input.size()-i+1);
input_vector.push_back(temp);
}
sort(input_vector.begin(), input_vector.end(), cmp);
for (int i = 0; i < input_vector.size(); i++) {
printf("%s\n", input_vector[i].c_str());
}
}
// system("pause");
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值