题解 | #字符串排序#

字符串排序

http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

#include<bits/stdc++.h>
using namespace std;
struct node{
    char c;
    int pos;
}nd;
bool cmp(node a,node b)
{
    if(isalpha(a.c)&&isalpha(b.c))
    {
        if(toupper(a.c)!=toupper(b.c))
        {
            return toupper(a.c)<toupper(b.c);
        }else if(toupper(a.c)==toupper(b.c))
        {
            return a.pos<b.pos;
        }
    }
    return true;
}
int main()
{
    string str;
    getline(cin,str);
    int len = str.length();
    char ch[len];
    strcpy(ch,str.data());
    vector<node> vec;
    char temp[10010];
    memset(temp,'|',sizeof(temp));//默认'|'不会出现
    
    for(int i = 0;i<len; i++)
    {
        if(isalpha(ch[i])){
            nd.c=ch[i];
            nd.pos = i;
            vec.push_back(nd);
        }else{
            temp[i] = ch[i];
        }
    }
    sort(vec.begin(),vec.end(),cmp);
//     for(vector<node>::iterator it = vec.begin();it != vec.end();it++)
//     {
//         printf("%c", it->c);
//     }
    //字母放进去
    vector<node>::iterator put = vec.begin();
    for(int i=0;i<len&&put!=vec.end();i++)
    {
        if(temp[i]=='|')
        {
            temp[i] = put->c;
            put++;
        }
    }
    
    for(int i = 0;i < len;i++){
        cout<<temp[i];
    }
    return 0;
}
全部评论

相关推荐

华为终究还是没走到最后,倒在了主管面,不甘心,不甘心啊
想去重庆的鸽子在吐槽:不用硬顶着17级台风上班了
点赞 评论 收藏
分享
牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
09-19 12:15
门头沟学院 Java
猫头夜鹰:请问收到意向要点接受拒绝吗,还是开奖之后再接受拒绝
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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