-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscs.cpp
More file actions
251 lines (186 loc) · 4.21 KB
/
Copy pathscs.cpp
File metadata and controls
251 lines (186 loc) · 4.21 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Student name:- Aparna Bhat
University :- University of Texas at Arlington
This program has been coded in c++
Execution command:- g++ scs.cpp -o scs.o
./scs.o
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <climits>
using namespace std;
int flag = 0;
int calcOverlappingPair(string s1, string s2, string& str)
{
int max = INT_MIN;
int m = s1.length();
int n = s2.length();
// check suffix of s1 matches with prefix of s2
for (int i = 1; i <= min(m, n); i++)
{
if (s1.compare(m - i, i, s2, 0, i) == 0)
{
if (max < i)
{
max = i;
str = s1 + s2.substr(i);
//cout << s1 << endl;
//cout << s2 << endl;
}
}
}
// check prefix of s1 matches with suffix of s2
for (int i = 1; i <= min(m, n); i++)
{
if (s1.compare(0, i, s2, n - i, i) == 0)
{
if (max < i)
{
max = i;
str = s2 + s1.substr(i);
}
}
}
return max;
}
string calculateShortestSuperstring(vector<string> arr) // no const, no-ref
{
int n = arr.size();
std::vector<string> temp = arr;
std::vector<string> result;
int maxArray[temp.size() - 1];
int m = 0;
int indexes[temp.size()];
int k = 0;
// run n-1 times to consider every pair
while (n != 1)
{
// to store maximum overlap
int max = INT_MIN;
// to store array index of strings involved in maximum overlap
int p, q;
// to store resultant string after maximum overlap
string res_str;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
string str;
// r will store maximum length of the matching prefix and
// suffix. str is passed by reference and store the result
// string after maximum overlap of arr[i] and arr[j], if any
int r = calcOverlappingPair(arr[i], arr[j], str);
// check for maximum overlap
if (max < r)
{
max = r;
res_str.assign(str);
p = i, q = j;
//cout << arr[i] << endl;
}
}
}
maxArray[m] = max;
m++;
flag = 1;
//cout << arr[p] << endl;
//cout << arr[q] << endl;
indexes[k] = p;
indexes[k + 1] = q;
k = k + 2;
//result.push_back(temp[p]);
//result.push_back(temp[q]);
//cout << arr[0] << endl ;
// ignore last element in next cycle
n--;
// if no overlap, append arr[n] to arr[0]
if (max == INT_MIN)
{
arr[0] += arr[n];
//cout << arr[0] << endl ;
}
else
{
// copy resultant string to index p
//cout << arr[p] << endl ;
//cout << arr[q] << endl ;
arr[p] = res_str;
// copy string at last index to index q
arr[q] = arr[n];
//cout << arr[p] << endl ;
//cout << arr[q] << endl ;
}
//cout << arr[0] << endl;
}
/*for(int i = 0 ; i < sizeof(indexes)/sizeof(indexes[0]) ; i++)
{
cout << indexes[i] << endl ;
}*/
for (int i = 0; i < sizeof(indexes) / sizeof(indexes[0]); i++)
{
int count = maxArray[i];
int spaceCount = temp[indexes[i]].size() - count;
for (int k = 0; k < spaceCount; k++)
{
cout << " ";
}
cout << temp[indexes[i]] << endl;
}
/*for (int i = 0 ; i < result.size() ; i++)
{
if(temp[i] == temp[p])
{
cout << temp[i] << endl;
for ( int j = i+1 ; j < arr.size() ; j ++)
{
if (temp[j] == temp[q])
{
cout << temp[j] << endl;
}
}
}
}*/
/*for (int i = 0 ; i < result.size() ; i++)
{
cout << result[i] << endl;
}*/
//cout << arr[p] << endl;
//cout << arr[q] << endl;
return arr[0];
}
// Shortest Superstring Problem
int main()
{
int n;
int i;
string str;
//vector<string> arr = { "1001001", "100102", "0010010", "110010" };
vector<string> input;
cout << "Please enter number of lines (not more than 100)" << endl;
cin >> n;
while (n > 100)
{
cout << "input lines cannot be greater than 100 . Please re-enter" << endl;
cin >> n;
}
cout << "Please enter the strings one per line " << endl;
for (i = 0; i < n; i++)
{
cin >> str;
while (str.length() > 50)
{
cout << "String length should not exceed 50 . please reenter this string " << endl;
cin >> str;
}
input.push_back(str);
}
/*for(i = 0 ; i < n ; i++)
{
cout << input[i] << endl;
}*/
string scs = calculateShortestSuperstring(input);
cout << "The Shortest Superstring is " << scs << endl;
cout << "Length is : " << scs.length() << endl;
return 0;
}