某道华为机试题

这道题比较简单,我就直接放题目跟我的解答吧

对输入的单词进行字典序排序输出: 字典序定义 1. 单词中字母比较不区分大小写,两个单词先以第一个字母作为排序的基准,
如果第一个字母相同,就用第二个字母为基准,如果第二个字母相同就以第三个字母为基准。依此类推,如果到某个字母不相同,
字母顺序在前的那个单词顺序在前。 2. 当一个短单词和一个长单词的开头部分都相同(即短单词是长单词从首字母开始的一部分),
短单词顺序在前。 3. 字母大小写不同的相同单词,只输出一次。
输入描述:
不超过255个字符中,单词间用空格进行分隔,为简单起见,单词不包含连字符,无其它标点符号
输出描述:
输出排序后的单词,单词之间用空格隔开(最后不带空格),重复的单词只输出一次。
示例1
输入

Hello hello world
输出

Hello world

i LOVE Cc I love CC Hello Hel Hellow
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iterator>
#include <cctype>
/***
1,思路为存入某一行的数据,利用strtok切分开
2,将切分开的数据导入查重操作,去掉重复性的数据
3,调用sort进行string数组的字典序排序,注意满足题意需要在cmp函数里面用些技巧
***/

using namespace std;
char data[255];      //存入控制台某一行的数据
string str[255];       //存储切分后的数据

int cmp(string a,string b)
{
    string dst_data,dst_temp;
    transform(a.begin(), a.end(), back_inserter(dst_data), ::toupper);
    transform(b.begin(), b.end(), back_inserter(dst_temp), ::toupper);
    return dst_data.compare(dst_temp)<0;
}

int CheckString(char *data,int num){  //解决重复,大小写问题
    string dst_data,dst_temp;
    string temp = data;
    transform(temp.begin(), temp.end(), back_inserter(dst_data), ::toupper);
    for(int i=0;i<num;i++){
        dst_temp = "";
        string each_str = str[i];
        transform(each_str.begin(), each_str.end(), back_inserter(dst_temp), ::toupper);
        if(dst_data==dst_temp){
            return 1;
        }
    }
    return 0;
}
int main()
{
    int count = 0;
    cin.getline(data,255);
    const char * split = " ";
    char * p;
    p = strtok (data,split);
    while(p!=NULL) {
        if(CheckString(p,count)){
            p = strtok(NULL,split);
            continue;
        }
        else{
            str[count++] = p;
            p = strtok(NULL,split);
        }
    }
    sort(str, str+count, cmp);
    for(int i=0;i<count-1;i++)        //格式控制
        cout<<str[i]<<" ";
    cout<<str[count-1];
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值