forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKingSort.cpp
More file actions
210 lines (199 loc) · 6.34 KB
/
KingSort.cpp
File metadata and controls
210 lines (199 loc) · 6.34 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// BEGIN CUT HERE
// END CUT HERE
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
// BEGIN CUT HERE
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
template<typename T> void print( T a ) {
cerr << a;
}
static void print( long long a ) {
cerr << a << "L";
}
static void print( string a ) {
cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
cerr << "{";
for ( int i = 0 ; i != a.size() ; i++ ) {
if ( i != 0 ) cerr << ", ";
print( a[i] );
}
cerr << "}" << endl;
}
template<typename T> void eq( int n, T have, T need ) {
if ( have == need ) {
cerr << "Case " << n << " passed." << endl;
} else {
cerr << "Case " << n << " failed: expected ";
print( need );
cerr << " received ";
print( have );
cerr << "." << endl;
}
}
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
if( have.size() != need.size() ) {
cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
print( have );
print( need );
return;
}
for( int i= 0; i < have.size(); i++ ) {
if( have[i] != need[i] ) {
cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
print( have );
print( need );
return;
}
}
cerr << "Case " << n << " passed." << endl;
}
static void eq( int n, string have, string need ) {
if ( have == need ) {
cerr << "Case " << n << " passed." << endl;
} else {
cerr << "Case " << n << " failed: expected ";
print( need );
cerr << " received ";
print( have );
cerr << "." << endl;
}
}
// END CUT HERE
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
vector<string> split( const string& s, const string& delim =" " ) {
vector<string> res;
string t;
for ( int i = 0 ; i != s.size() ; i++ ) {
if ( delim.find( s[i] ) != string::npos ) {
if ( !t.empty() ) {
res.push_back( t );
t = "";
}
} else {
t += s[i];
}
}
if ( !t.empty() ) {
res.push_back(t);
}
return res;
}
map<string, int> mp1;
map<int, string> mp2;
class King {
public:
string name;
string strVal;
int val;
string ful;
King(string _n, string _v, string _f) : name(_n), strVal(_v), ful(_f) {
val = mp1[strVal];
}
bool operator<(const King& rhs) const {
if (name != rhs.name) return name < rhs.name;
return val < rhs.val;
}
};
string mm[14] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XX", "XXX", "XL", "L"};
int dd[14] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50};
void init() {
mp1.clear();
mp2.clear();
REP(i,14) {
mp1[mm[i]] = dd[i];
mp2[dd[i]] = mm[i];
}
for (int t = 10; t <= 40; t += 10) {
FOR(i,1,9) {
string s = mp2[t] + mp2[i];
mp1[s] = t + i;
mp2[t + i] = s;
}
}
}
class KingSort {
public:
vector <string> getSortedList(vector <string> kings) {
init();
vector<King> ks;
REP(i,kings.size()) {
vector<string> parts = split(kings[i], " ");
ks.push_back(King(parts[0], parts[1], kings[i]));
}
sort(ks.begin(), ks.end());
vector <string> res;
REP(i,ks.size()) {
res.push_back(ks[i].ful);
}
return res;
}
};
// BEGIN CUT HERE
int main() {
{
string kingsARRAY[] = {"Louis IX", "Louis VIII"};
vector <string> kings( kingsARRAY, kingsARRAY+ARRSIZE(kingsARRAY) );
string retrunValueARRAY[] = {"Louis VIII", "Louis IX" };
vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) );
KingSort theObject;
eq(0, theObject.getSortedList(kings),retrunValue);
}
{
string kingsARRAY[] = {"Louis IX", "Philippe II"};
vector <string> kings( kingsARRAY, kingsARRAY+ARRSIZE(kingsARRAY) );
string retrunValueARRAY[] = {"Louis IX", "Philippe II" };
vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) );
KingSort theObject;
eq(1, theObject.getSortedList(kings),retrunValue);
}
{
string kingsARRAY[] = {"Richard III", "Richard I", "Richard II"};
vector <string> kings( kingsARRAY, kingsARRAY+ARRSIZE(kingsARRAY) );
string retrunValueARRAY[] = {"Richard I", "Richard II", "Richard III" };
vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) );
KingSort theObject;
eq(2, theObject.getSortedList(kings),retrunValue);
}
{
string kingsARRAY[] = {"John X", "John I", "John L", "John V"};
vector <string> kings( kingsARRAY, kingsARRAY+ARRSIZE(kingsARRAY) );
string retrunValueARRAY[] = {"John I", "John V", "John X", "John L" };
vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) );
KingSort theObject;
eq(3, theObject.getSortedList(kings),retrunValue);
}
{
string kingsARRAY[] = {"Philippe VI", "Jean II", "Charles V", "Charles VI", "Charles VII", "Louis XI"};
vector <string> kings( kingsARRAY, kingsARRAY+ARRSIZE(kingsARRAY) );
string retrunValueARRAY[] = {"Charles V", "Charles VI", "Charles VII", "Jean II", "Louis XI", "Philippe VI" };
vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) );
KingSort theObject;
eq(4, theObject.getSortedList(kings),retrunValue);
}
{
string kingsARRAY[] = {"Philippe II", "Philip II"};
vector <string> kings( kingsARRAY, kingsARRAY+ARRSIZE(kingsARRAY) );
string retrunValueARRAY[] = {"Philip II", "Philippe II" };
vector <string> retrunValue( retrunValueARRAY, retrunValueARRAY+ARRSIZE(retrunValueARRAY) );
KingSort theObject;
eq(5, theObject.getSortedList(kings),retrunValue);
}
return 0;
}
// END CUT HERE