forked from aseembrahma/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrIIRec.cpp
177 lines (152 loc) · 4.34 KB
/
StrIIRec.cpp
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
#include <iostream>
#include <iterator>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <sstream>
#include <queue>
#include <string>
#include <cmath>
#include <set>
#include <limits>
#include <numeric>
#include <bitset>
#include <stack>
#include <utility>
#include <unordered_set>
#include <unordered_map>
#define DEBUG 0
#define ARR_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
#define INF_INT numeric_limits<int>::max()
#define MIN_INT numeric_limits<int>::min()
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> pii;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
template <typename K, typename V>
V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) {
typename std::map<K,V>::const_iterator it = m.find( key );
if ( it == m.end() ) {
return defval;
}
else {
return it->second;
}
}
template<typename T>
void print_vector(const vector<T> &v) {
cerr << " { ";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
cerr << *it << ", ";
}
cerr << " } " << endl;
}
class StrIIRec {
/*
* When looking at the ith tree the max diff should atleast be i, so as to be
* able to support an ordering like x, x+1, x+2, ...
*/
public:
string recovstr(int n, int minInv, string minStr) {
string result(minStr);
// add any missing letters
for (int i = 0; i < n; ++i)
if (result.find('a' + i) == string::npos)
result += ('a' + i);
// convert to numbers, for easier representation
vector<int> v;
for (int i = 0; i < result.size(); ++i)
v.push_back(result[i] - 'a');
// count the number of inversions
vector<int> inv(v.size());
int total_inv = 0;
for (int i = 0; i < v.size() - 1; ++i) {
if (i == 0)
inv[i] = 0;
else
inv[i] = inv[i - 1];
for (int j = i + 1; j < v.size(); ++j) {
if (v[j] < v[i]) {
++inv[i];
++total_inv;
}
}
}
if (total_inv < minInv) {
// find out how many letters from the end we need to reverse
int i, prev_total_inv = inv.back();
for (i = 2; i <= n; ++i) {
total_inv = (i < n) ? inv[v.size() - i - 1] : 0;
total_inv += ((i * (i - 1)) / 2);
if (total_inv >= minInv) {
if (total_inv > minInv) {
--i;
total_inv = prev_total_inv;
}
break;
}
prev_total_inv = total_inv;
}
//cout << i << ", " << total_inv << endl;
sort(v.begin() + (v.size() - i), v.end(), greater<int>());
--i;
while (total_inv < minInv) {
//print_vector(v);
int j;
for (j = v.size() - 2; j >= 0 && v[j] > v[j + 1]; --j);
for (int k = v.size() - 1; k > j; --k) {
if (v[k] > v[j]) {
swap(v[j], v[k]);
break;
}
}
reverse(v.begin() + j + 1, v.end());
total_inv = 0;
for (int k = 0; k < v.size() - 1; ++k)
for (int l = k + 1; l < v.size(); ++l)
if (v[l] < v[k])
++total_inv;
}
}
for (int i = 0; i < v.size(); ++i)
result[i] = ('a' + v[i]);
return result;
}
bool runTest(int n, int minInv, string minStr, string solution) {
string output = recovstr(n, minInv, minStr);
if (output != solution)
{
cerr << "Failed for " << n << ", " << minInv << ", " << minStr << endl;
cerr << "Expected: " << solution << endl;
cerr << "Found instead: " << output << endl;
return false;
}
else {
return true;
}
}
};
int main(int argc, char* argv[]) {
StrIIRec test;
{
test.runTest(2, 1, "ab", "ba");
test.runTest(9, 1, "efcdgab", "efcdgabhi");
test.runTest(11, 55, "debgikjfc", "kjihgfedcba");
test.runTest(15, 0, "e", "eabcdfghijklmno");
test.runTest(9, 20, "fcdebiha", "fcdehigba");
}
}