-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJoinedString.cpp
More file actions
205 lines (170 loc) · 3.7 KB
/
JoinedString.cpp
File metadata and controls
205 lines (170 loc) · 3.7 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
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <sstream>
#include <set>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <time.h>
using namespace std;
/* http://topcoder.bgcoder.com/print.php?id=1244 */
#define SIZEOFSTRINGARRAY(s) sizeof(s)/sizeof(s[0])
string memo[12][1<<12];
string common[12][12];
class JoinedString
{
size_t len;
string dp(int current, int mask);
public:
string joinWords(vector<string> words);
};
string JoinedString::dp(int current, int mask)
{
string &memoval = memo[current][mask];
if (memoval != "") return memoval;
if (!mask) return "";
for (size_t i = 0, size = len; i < size; ++i)
{
if (mask & (1 << i))
{
string temp = common[current][i] + dp(i, mask ^ (1 << i));
if (memoval == "" || (temp.length() < memoval.length() || temp.length() == memoval.length() && temp < memoval))
{
memoval = temp;
}
}
}
return memoval;
}
string JoinedString::joinWords(vector<string> wordsList)
{
len = wordsList.size();
vector<string> words;
for (size_t i = 0; i < len; ++i)
{
bool ok = true;
for (size_t j = 0; j < len; ++j)
{
common[i][j] = "";
if (i != j)
{
string w1 = wordsList[i];
string w2 = wordsList[j];
if (w2.find(w1) != -1)
{
ok = false;
break;
}
}
}
if (ok)
{
words.push_back(wordsList[i]);
}
}
len = words.size();
for (int i = 0; i < len; ++i)
{
for (int j = 0; j < 1<<len; ++j)
{
memo[i][j] = "";
}
}
for (size_t i = 0; i < len; ++i)
{
for (size_t j = 0; j < len; ++j)
{
if (i != j)
{
common[i][j] = words[j];
for (size_t t = 1, size = std::min(words[i], words[j]).length(); t < size; ++t)
{
string s1 = words[i].substr(t, words[i].length() - t);
string s2 = words[j].substr(0, words[i].length() - t);
if (s1.compare(s2) == 0)
{
common[i][j] = words[j].substr(s2.length(), words[j].length() - s2.length() + 1);
break;
}
}
}
}
}
string best = "";
for (size_t i = 0; i < len; ++i)
{
string temp = words[i] + dp(i, ((1 << len) - 1) ^ (1 << i));
if (best == "" || (temp.length() < best.length() || temp.length() == best.length() && temp < best))
{
best = temp;
}
}
return best;
}
void TEST(vector<string> words, string expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
JoinedString joinedString;
string result = joinedString.joinWords(words);
assert( result.compare(expected) == 0 );
end = clock();
cpu_time_used = ((double) (end - start));
cout<<"Time taken : "<<cpu_time_used<<endl;
}
vector<string> convert(string *list, int n)
{
vector<string> ret;
for (int i = 0; i< n; ++i)
{
ret.push_back(list[i]);
}
return ret;
}
void Test1()
{
string test[] = {"BAB", "ABA"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), "ABAB");
}
void Test2()
{
string test[] = {"ABABA", "AKAKA", "AKABAS", "ABAKA"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), "ABABAKAKABAS");
}
void Test3()
{
string test[] = {"AAA","BBB", "CCC", "ABC", "BCA", "CAB"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), "AAABBBCABCCC");
}
void Test4()
{
string test[] = {"OFG", "SDOFGJTILM", "KBWNF", "YAAPO", "AWX", "VSEAWX", "DOFGJTIL", "YAA"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), "KBWNFSDOFGJTILMVSEAWXYAAPO");
}
void Test5()
{
string test[] = {"STRING", "RING"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), "STRING");
}
void Test6()
{
}
void Test7()
{
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
cout<<"success";
getchar();
return 0;
}