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 "not Stable" 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
3AA 10bb 10cc 20cc 20bb 10aa 103aa 10bb 10cc 20cc 20aa 10bb 103aa 10bb 10cc 20aa 10bb 10cc 20
Sample Output
Not STABLECC 20aa 10bb 10RIGHTERRORCC 20aa 10BB 10
The problem: first according to the requirements of the topic, write a stable sort, (note here if the score is the same as the first appear in front, instead of the name of the dictionary order), and then to compare.
AC Code
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 6 structNood7 {8 Charname[ -];9 intscore;Ten intdata; One} student[3300], solution[3300]; A - BOOLCMP (Nood A, nood b) - { the if(A.score = =B.score) - { - returnA.data <B.data; - } + Else - { + returnA.score >B.score; A } at } - - intMain () - { - intN; - BOOLFlag1, Flag2; in while(~SCANF ("%d", &N)) - { toFlag1 = Flag2 =true; + for(inti =0; I < n; i++) - { thescanf"%s%d", Student[i].name, &student[i].score); *Student[i].data =i; $ }Panax Notoginseng for(inti =0; I < n; i++) - { thescanf"%s%d", Solution[i].name, &solution[i].score); + } ASort (student, student+N, CMP); the for(inti =0; I < n; i++) + { - if(Solution[i].score! =Student[i].score) $ { $Flag1 =false; - Break; - } the } - for(inti =0; I < n; i++)Wuyi { the intNAME_CMP =strcmp (Solution[i].name, student[i].name); - if(NAME_CMP) Wu { -Flag2 =false; About Break; $ } - } - if(Flag1 &&Flag2) -printf"right\n"); A Else if(Flag1 &&!)Flag2) + { theprintf"Not stable\n"); - for(inti =0; I < n; i++) $printf"%s%d\n", Student[i].name, student[i].score); the } the Else if(!flag1 &&!)Flag2) the { theprintf"error\n"); - for(inti =0; I < n; i++) inprintf"%s%d\n", Student[i].name, student[i].score); the } the } About the return 0; the}
View Code
E-Stable sequencing (struct)