Stable sorting
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5762 Accepted Submission (s): 2159
Problem Description
As we all know, fast sorting is an unstable sort method.
If there is any a[i],a[j] (i<j) in the array, where a[i]==a[j] before sorting a[i] must appear before a[j], the sort is considered stable.
A university admissions office gets a list of results, which record the candidate's name and test results. And it uses a sort algorithm to decrease the order by grade. Now, please judge if the sorting algorithm is correct, and if correct, determine if the sorting algorithm is stable.
Input
This topic contains multiple sets of inputs, please handle to the end of the file.
For each set of data, the first row has a positive integer N (0<n<300), which represents the number of candidates in the score list.
Next there are n lines, each line has a string representing the examinee's name (not more than 50, only ' a ' ~ ' Z '), and an integer representing the examinee score (less than 500). Where the name and result are separated by a space.
Then there are n rows, which is a sequence of the above list that is generated after a sort algorithm. Format Ibid.
Output
For each set of data, if the algorithm is correct and stable, it outputs "right" in a row. If the algorithm is correct but not stable, output "notstable" in a row, and output the correct stable sorted list below, formatted with the input. If the algorithm is wrong, output "error" in a row, and output the correct stable sorted list below, formatted with the input.
Note that this topic does not consider that the sorting algorithm is wrong, but the result is correct for such an unexpected situation.
Sample Input
3
AA 10
BB 10
CC 20
CC 20
BB 10
AA 10
3
AA 10
BB 10
CC 20
CC 20
AA 10
BB 10
3
AA 10
BB 10
CC 20
AA 10
BB 10
CC 20
Sample Output
Not Stable
CC 20
AA 10
BB 10
Right
Error
CC 20
AA 10
BB 10
Author
Linle
Source
2008 Zhejiang University Postgraduate second-round warm-up (2)--Full true simulation
Recommend
LCY | Wehave carefully selected several similar problems for you:1862 1873 1876 2020 1106
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 structnode{7 intId,socre;8 Charname[ -];9 BOOL operator< (ConstNODE a)Const{Ten if(Socre!=a.socre)returna.socre<Socre; One returnid<a.id; A } -}s[305],t[305]; - intN; the intMain () - { - while(SCANF ("%d", &n)! =EOF) { - for(intI=0; i<n;i++){ +scanf"%s%d",s[i].name,&s[i].socre); -S[i].id=i; + } ASort (s,s+n); at BOOLflag1=false, flag2=false; - for(intI=0; i<n;i++){ -scanf"%s%d",t[i].name,&t[i].socre); - if(strcmp (s[i].name,t[i].name)) { - if(S[i].socre==t[i].socre) flag1=true; - ElseFlag2=true; in } - } to if(FLAG2) printf ("error\n"); + Else if(FLAG1) printf ("Not stable\n"); - Else{printf ("right\n");Continue; } the for(intI=0; i<n;i++) *printf"%s%d\n", s[i].name,s[i].socre); $ }Panax Notoginseng return 0; -}
Double keyword Sort ~ ~
HDU 1872 Stable Sequencing