Problem Description
Problem Analysis
Sort the structs
then compare with the input
Check whether the sorting is exactly the same
“Not Stable” means the scores are correct but the names are not sorted correctly
Note that when scores are equal
the correct order should put earlier inputs first
so you can record the input index
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node {
char s[55];
int x,id;
} a[320],b[320];
bool cmp(node A,node B) {
if(A.x==B.x)
return A.id<B.id;
// return strcmp(A.s,B.s)<0;
else
return A.x>B.x;
}
int main() {
int n;
while(scanf("%d",&n)!=EOF) {
for(int i=0; i<n; i++) {
scanf("%s %d",&a[i].s,&a[i].x);
a[i].id=i;
}
sort(a,a+n,cmp);
int flag=0;
for(int i=0; i<n; i++) {
scanf("%s %d",&b[i].s,&b[i].x);
b[i].id=0;
if(flag<2) {
if(strcmp(a[i].s,b[i].s)!=0)
flag=1;
if(b[i].x!=a[i].x)
flag=2;
}
}
if(flag==0)
printf("Right\n");
else {
if(flag==1)
printf("Not Stable\n");
else if(flag==2)
printf("Error\n");
for(int i=0; i<n; i++)
printf("%s %d\n",a[i].s,a[i].x);
}
}
return 0;
}
|
Problem link: 【HDU】[1872]Stable Sort