HDU--1872 stable sorting, hdu -- 1872 stable sorting

Source: Internet
Author: User

HDU--1872 stable sorting, hdu -- 1872 stable sorting
As we all know, quick sorting is an unstable sorting method.
If any aii, ajj (I <j), and aii = ajj appear in the array, aii must appear before ajj after sorting, the sorting is stable.

A university admissions office obtains a score list, which records the names and scores of the candidates. In addition, a sort algorithm is used to perform descending sorting by score. Check whether the Sorting Algorithm is correct. If so, check whether the Sorting Algorithm is stable. The Input question contains multiple groups of inputs. Please process it until the end of the file.
For each group of data, the first row has a positive integer N (0 <N <300), representing the number of candidates in the Score list.
Next, there are N rows. Each row has a string representing the examinee's name (length not more than 50, only including 'A '~ 'Z'), and an integer representing the score (less than 500 ). The names and scores are separated by a space.
Next there are N rows, which are a sequence generated after the list goes through a sorting algorithm. The format is the same as above. Output outputs "Right" in a row if the algorithm is correct and stable for each group of data ". If the algorithm is correct but Not Stable, "Not Stable" is output in a row, and a list of correct and Stable sorting is output below. The format is the same as that of the input. If the algorithm is incorrect, "Error" is output in a row, and a list of correct and stable sorting is output below, in the same format as the input.

Note that this sorting algorithm is incorrect, but the result is correct. Sample Input aa 10

bb 10cc 20cc 20bb 10aa 103aa 10bb 10cc 20cc 20aa 10bb 103aa 10bb 10cc 20aa 10bb 10cc 20

Sample output

Not Stablecc 20aa 10bb 10 RightErrorcc 20aa 10bb 10

Question:
The direct sorting of sort may be unstable.
Question:
The scores are not the same. sort is sorted from high to low. If the scores are the same, the IDs of the input names are sorted in sequence (not in lexicographically ordered order, but by name, that is, the beginning is in front, and the back is in front. "This place has been lost for a long time, and it is regarded as a lexicographic sorting ...")
AC code:
 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 struct stu 6 {  int id;  7    char num[51]; 8    int score; 9 }p[300],q[300];10 bool cmp(stu x,stu y)11 {12    if(x.score!=y.score)13        {if(x.score>y.score)14           return true;15         else return false;   16         }17    else 18     {if(x.id<y.id)19        return true;20       else return false;  21       22     }  23 }24 int main()25 {26 int n,i,j,k;27 while(scanf("%d",&n)!=EOF)28   {for(i=0;i<n;i++)29      {scanf("%s %d",p[i].num,&p[i].score);30       p[i].id=i;     31      }32    for(i=0;i<n;i++)33      scanf("%s %d",q[i].num,&q[i].score);  34    sort(p,p+n,cmp);35    j=0;36    k=0;37 38    for(i=0;i<n;i++)39     {if(strcmp(p[i].num,q[i].num)==0) 40       j++;41       if(p[i].score==q[i].score)       42       k++;                                                           43     }44    if(j==n&&k==n) printf("Right\n");  45     else if(k==n&&j!=n) 46    {printf("Not Stable\n");47     for(i=0;i<n;i++)48     printf("%s %d\n",p[i].num,p[i].score);49    } 50    else  51    {printf("Error\n");52     for(i=0;i<n;i++)53     printf("%s %d\n",p[i].num,p[i].score);54        55    }56   }57 return 0;58 }

 




 

Related Article

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.