51nod 1954 交叉排序

题目

给出一个序列,这个序列中每一个元素要么是一个英文单词,要么是一个整数。除了最后一个元素,每一个元素后面有一个逗号,再跟一个空格。最后一个元素后面有一个句号。

现在要对这个序列进行排序,把所有的单词按照字典序升序排序,所有的整数按照升序排序。并且保持原来是整数的位置还是整数,原来是单词的位置还是单词。单词比较的时候忽略大小写。

输入
单组测试数据。
输入若干个元素,对于单词,非空且长度不超过10,只由大小写字母组成;对于整数,绝对值<=1000000。元素个数不超过10000,序列中至少有一个元素。
输出
输出排序之后的序列。按照输入的格式。
输入样例
样例输入1
banana, strawberry, OrAnGe.
样例输入2
Banana, StRaWbErRy, orange.
输出样例
样例输出1
banana, OrAnGe, strawberry.
样例输出2
Banana, orange, StRaWbErRy.

解题思路

利用两个vector容器一个存单词一个存数字 再利用一个数组表示第几位是数字还是单词

代码

#include <bits/stdc++.h>
#include<iostream>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include<cstring>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
#define INT_MAX 0x7fffffff
#define INT_MIN 0x80000000
const int MOD = 1E9+7;
const int N = 100000+5;
using namespace std;
vector<string>word;
vector<int>num;
int flag[N];


int cmps (string a, string b)
{
    for(int i = 0; i < a.length(); i++)
    {
        a[i] = tolower(a[i]);
    }
    for(int i = 0; i < b.length(); i++)
    {
        b[i] = tolower(b[i]);
    }
    return a > b;

}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    //freopen("input.txt","r",stdin);
    // IO
    int n,k=0,cnt = 0;

    string str;

    while(cin >> str){
        string ch;
        if ((str[0] < '0' || str[0] > '9')&&str[0]!='-'){

            ch.assign(str,0,(int)(str.length()-1));
            word.push_back(ch);
            flag[k] = 1;
        }
        else{
            ch.assign(str,0,(int)(str.length()-1));
            int b = atoi(ch.c_str());
            num.push_back(b);
            flag[k] = 0;
        }
        k++;
        if(str[(int)str.length()-1] == '.')
        {
            break;
        }
    }
    sort(word.begin(),word.end(),cmps);
    sort(num.rbegin(),num.rend());
    for(int i = 0; i < k; i++){
        if(flag[i]==0 && i == k - 1){
            cout << num.back() << '.';
            num.pop_back();
        }
        else if(flag[i]==1 && i == k - 1){
            cout << word.back() << '.';
            word.pop_back();
        }
        else if(flag[i]==0){
            cout << num.back() << ',' << ' ';
            num.pop_back();
        }
        else {
            cout << word.back() << ',' << ' ';
            word.pop_back();
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值