牛客网_华为机试_014_字串的连接最长路径查找(字典排序)

63 篇文章 1 订阅
28 篇文章 0 订阅

注意:该题目与标题不符,应该是牛客网的锅

题目描述

给定n个字符串,请对n个字符串按照字典序排列。 
输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。


输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
示例1

输入

9
cap
to
cat
card
two
too
up
boat
boot

输出

boat
boot
cap
card
cat
to
too
two
up

题目地址:https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723?tpId=37&tqId=21237&tPage=1&rp=&ru=%2Fta%2Fhuawei&qru=%2Fta%2Fhuawei%2Fquestion-ranking


这道题目严格来说OJ有问题:

不通过
您的代码已保存
答案错误:您提交的程序没有通过所有的测试用例
case通过率为0.00%

测试用例:
19
grLaArEX
B
Gc
MnuvCWc
kOmHJX
Qf
gNI
GRXvbgg
gMojlYPCzL
ToxnNKC
p
JG
oqojpxLUF
ZoTlmaSRT
VZfrxw
oBRWVGVN
Y
q
RsnLwtcV

对应输出应该为:

B
GRXvbgg
Gc
JG
MnuvCWc
Qf
RsnLwtcV
ToxnNKC
VZfrxw
Y
ZoTlmaSRT
gMojlYPCzL
gNI
grLaArEX
kOmHJX
oBRWVGVN
oqojpxLUF
p
q

可以看出OJ只是根据ASCII码进行排序而非严格的字典排序


思路一:利用c++泛型算法algorithm里的sort即可实现字典排序

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    int N = 0;
    while(cin >> N){
        vector<string> array;
        while(N>0){
            string temp;
            cin >> temp;
            array.push_back(temp);
            N--;
        }
        sort(array.begin(), array.end());
        for(string s : array)
            cout << s << endl;
    }
    return 0;
}

严格字典排序(无法通过OJ):

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//严格字典排序,此代码保证字典排序,但无法通过OJ
//编写自己的比较函数,进行sort
bool strcompare(string a, string b)
{
	//将a、b全部转化为大写,进行比较
	for (char &c : a)
		c = toupper(c);
	for (char &c : b)
		c = toupper(c);
	return a < b;    //升序排列
}

int main()
{
    int N = 0;
    while(cin >> N){
        vector<string> array;
        while(N>0){
            string temp;
            cin >> temp;
            array.push_back(temp);
            N--;
        }
        sort(array.begin(), array.end(), strcompare);
        for(string s : array)
            cout << s << endl;
    }
    return 0;
}



思路二:纯C风格字符串操作,等排序算法总结完后补充:



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值