Huawei oj-String Sorting

Source: Internet
Author: User

http://career-oj.huawei.com/exam/ShowProblemInfo?id=2168

Write a program that sorts the characters in the input string according to the following rules.

Rule 1: English letters are arranged from A to Z, and are not case sensitive.

For example, input: type output: Epty

Rule 2: When the case of the same English letter exists, it is arranged in the order of entry.

For example, input: BabA output: aABb

Rule 3: Other characters of non-English letters remain in their original position.

For example, input: By?e output: Be?y

Examples:

Input:

A Famous Saying:much Ado About Nothing (2012/8).

Output:

A aaaabc Dfgghh:iimm nNn oooos sttuuuy (2012/8).

Answer:

is actually a small-to-large sort.

Rule 2 means that the sorting algorithm must be a stable algorithm, bubbling can be, the choice is not stable. This has to be seen!

Rule 3 Basically can be ignored, as long as the bubble sort when the judge two is a letter and ToLower (CH) before the small exchange can be, so do not control those non-letter.

Summarize:

Think about whether there is a better way to do it before you start. My practice is to extract all the letters from the original string (non-letters remain), put them in a newly opened array, sort them, and then put the ordered ones back into the strings that don't keep the letters. The space efficiency here is very low. My low-efficiency code:

#include <iostream>#include<string>#include<cctype>using namespacestd;voidMysort (Char* PARR,intn);//#define DEBUGintMainvoid){    stringstr;    Getline (CIN, str); intLen =str.length (); intnochcnt =0;//Non-English letter number    Char* PARR =New Char[Len +1];  for(inti =0; I <= Len; i++)//result set is emptyParr[i] =' /'; //first fill non-string into result Parr     for(inti =0; i < Len; i++)    {        if((Str[i] >= $&& Str[i] <= -) || (Str[i] >= the&& Str[i] <=122))        {            Continue; }        Else{Parr[i]=Str[i]; Nochcnt++; }    }    //extract the characters to be sorted    Char* Psortarr =New Char[Len-nochcnt]; intII =0;  for(intj =0; J < Len; J + +)    {        if(' /'==Parr[j]) psortarr[ii++] =Str[j]; } #ifdef DEBUG cout<< Psortarr <<Endl;#endif    //sort belowMysort (Psortarr, Len-nochcnt); #ifdef DEBUG cout<< Psortarr <<Endl;#endif    //fill in the resultsII =0;  for(intj =0; J < Len; J + +)    {        if(' /'==Parr[j]) parr[j]= psortarr[ii++]; } cout<<PARR;    Delete[] Psortarr; Delete[] PARR; #ifdef DEBUG while(true) cin.Get();#endif    return 0;}//Note: This sort algorithm must be stable, otherwise it does not satisfy the rule 2voidMysort (Char* PARR,intN) {    //Bubble Sort     for(inti =0; I < n-1; i++)    {         for(intj =0; J < N-i-1; J + +)        {            if(ToLower (Parr[j +1]) <ToLower (Parr[j])) {                CharCH = parr[j +1]; Parr[j+1] =Parr[j]; PARR[J]=ch; }        }    }}
View Code

Huawei oj-String Sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.