大小写字母和数字按字典排序

大小写字母和数字按字典排序

  • 问题

问题

一、请对一组字符串进行排序,字符串由大小写字母和数字组成,需要满足以下比较规则:
1、长度不同时,长度较短在排前面;
2、长度相同时,按照字典顺序排列(AaBb - Zz,0-9顺序),即大写字母在小写字母前,数字排在字母后。
具体例子:
输入:

abc Abc 123 11bc CBD abcd a

排序后结果为:

a 1 Abc abc CBD 1bc 123 abcd

要求时间复杂度为:

O(nlogn)

大小写字母和数字按字典排序_第1张图片
Code

//c++
#include 
#include 
#include 
#include 

using namespace std;

void sortAsSize(std::vector<string> &stri)
{
    for (int i = 0; i < stri.size(); i++)
    {
        for (int j = 0; j < stri.size() - i - 1; j++)
        {
            int t_int = 0;
            string t_str;
            if (stri.at(j).size() > stri.at(j + 1).size())
            {
                t_str = stri.at(j);
                stri.at(j) = stri.at(j + 1);
                stri.at(j + 1) = t_str;
            }
        }
    }
}

void sortAsChar(std::vector<string> &ss, int pos)
{
    std::vector<string> st = ss;
    int strsize = st.at(0).size();
    ss.clear();
    string Astr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string astr = "abcdefghijklmnopqrstuvwxyz";
    string nstr = "0123456789";
    std::vector<string>::iterator it;
    for (int i = 0; i < Astr.size(); ++i)  //for AcBbCc........
    {
        int locals = 0;
        std::vector<string> tt;
        for (int j = 0; j < st.size(); ++j)
        {
            if(st.at(j).at(pos) == Astr[i])
            {
                tt.push_back(st.at(j));
                locals ++;
            }
        }

        if(locals > 1 && pos < strsize - 1)
        {
            sortAsChar(tt, pos + 1);
        }

        for (it = tt.begin(); it != tt.end(); it++)
        {
            ss.push_back(*it);
        }

        locals = 0;
        tt.clear();

        for (int k = 0; k < st.size(); ++k)
        {
            if(st.at(k).at(pos) == astr[i])
            {
                tt.push_back(st.at(k));
                locals++;
            }
        }

        if(locals > 1 && pos < strsize - 1)
        {
            sortAsChar(tt, pos + 1);
        }

        for (it = tt.begin(); it != tt.end(); it++)
        {
            ss.push_back(*it);
        }
    }

    for (int m = 0; m < nstr.size(); ++m)  //for 01234....
    {
        int locals = 0;
        std::vector<string> tt;
        tt.clear();
        for (int l = 0; l < st.size(); ++l)
        {
            if(st.at(l).at(pos) == nstr[m])
            {
                tt.push_back(st.at(l));
                locals++;
            }
        }

        if(locals > 1 && pos < strsize - 1)
        {
            sortAsChar(tt, pos + 1);
        }

        for (it = tt.begin(); it != tt.end(); it++)
        {
            ss.push_back(*it);
        }
    }
}


int main()
{
    string strcin;
    while(getline(cin,strcin))   //abc Abc 123 1 1bc CBD abcd a  //abc Abc 123 1bc CBD
    {
        string temps ;
        string str ;
        std::vector<string> strs;
        std::vector<string> strt;
        std::vector<std::vector<string>> sss;
        std::vector<std::vector<string>> sst;
        std::vector<string>::iterator it;
        std::vector<std::vector<string>>::iterator its;

        str = strcin + ' ';
        for (int k = 0; k < str.size(); k++)
        {
            char tc = str[k];

            if(tc != ' ')
            {
                temps.push_back(tc);
            } else
            {
                strs.push_back(temps);
                temps.clear();
            }
        }

        sortAsSize(strs);   //sort as size of string

        cout << "\ncounts of string = " << strs.size() << endl<<endl;    //debug only

        for (it = strs.begin(); it != strs.end(); it++)
        {
            cout << *it << "鈥?" << (*it).size() <<")\t";    //debug only
        }

        cout<<endl;

        for (it = strs.begin(); it != strs.end(); it++)
        {
            if(it == strs.begin())
            {
                strt.push_back(*it);
            }
            else if(it == (strs.end() - 1))
            {
                if((*it).size() == (*(it - 1)).size())
                {
                    strt.push_back(*it);
                    sss.push_back(strt);
                } else
                {
                    sss.push_back(strt);
                    strt.clear();
                    strt.push_back(*it);
                    sss.push_back(strt);
                    strt.clear();
                }
            }
            else
            {
                if((*it).size() == (*(it - 1)).size())
                {
                    strt.push_back(*it);
                } else
                {
                    sss.push_back(strt);
                    strt.clear();
                    strt.push_back(*it);
                }
            }

        }

        sst.clear();
        for (its = sss.begin(); its != sss.end(); its++)
        {
            strt.clear();
            strt = *its;
            sortAsChar(strt,0);
            sst.push_back(strt);
        }

        str.clear();
        for (its = sst.begin(); its != sst.end(); its++)
        {
            for (it = (*its).begin(); it != (*its).end(); it++)
            {
                if(its == (sst.end() - 1) && it == ((*its).end() - 1))
                {
                    str += *it;
                }
                else
                {
                    str += *it + ' ';
                }
            }
        }

        cout<<endl;
        cout << str << endl;

    }
    return 0;
}

你可能感兴趣的:(大小写字母和数字按字典排序)