-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_E.cpp
More file actions
183 lines (171 loc) · 3.68 KB
/
05_E.cpp
File metadata and controls
183 lines (171 loc) · 3.68 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
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
struct info
{
int count;
char string[5];
};
int lexi(char string1[], char string2[])
{
for (int i = 0;; i++)
{
if (string1[i] != string2[i])
{
return string1[i] < string2[i] ? -1 : 1;
}
if (string1[i] == 0)
{
return 0;
}
}
}
int strl(char x[])
{
int i = 0;
while (x[i] != 0)
{
i++;
}
return i;
}
void strcpy1(char (&dest)[5], char (&source)[5])
{
for (int i = 0; i < 5; i++)
{
dest[i] = source[i];
}
}
void merge(char arr[][5], int l, int m, int r)
{
int length1 = m - l + 1;
int length2 = r - m;
// Create temp arrays
char L[length1][5], R[length2][5];
// Copy data to temp arrays L[] and R[]
for (int i = 0; i < length1; i++)
{
strcpy1(L[i], arr[l + i]);
}
for (int j = 0; j < length2; j++)
{
strcpy1(R[j], arr[m + 1 + j]);
}
// Merge the temp arrays back into arr[l..r]
int i = 0, j = 0, k = l;
while (i < length1 && j < length2)
{
if (lexi(L[i], R[j]) <= 0)
{
strcpy1(arr[k], L[i]);
i++;
}
else
{
strcpy1(arr[k], R[j]);
j++;
}
k++;
}
// Copy the remaining elements of
// L[], if there are any
while (i < length1)
{
strcpy1(arr[k], L[i]);
i++;
k++;
}
// Copy the remaining elements of
// R[], if there are any
while (j < length2)
{
strcpy1(arr[k], R[j]);
j++;
k++;
}
}
// l is for left index and r is
// right index of the sub-array
// of arr to be sorted */
void mergeSort(char arr[][5], int l, int r)
{
if (l >= r)
{
return; //returns recursively
}
int m = l + (r - l) / 2;
//sorting the left side
mergeSort(arr, l, m);
//sorting the right side
mergeSort(arr, m + 1, r);
//then marging both to get the original array
merge(arr, l, m, r);
}
int main()
{
char str[1000000 + 1];
scanf("%s", str);
int n = strl(str) - 3;
// printf("%d\n",n);
// if no substring of length four can be generated then there is no ans
if (n <= 0)
{
printf("NO SUCH STRING\n");
return 0;
}
char substrs[n][5];
//Generating substrings
for (int i = 0; i < n; i++)
{
substrs[i][0] = str[i];
substrs[i][1] = str[i + 1];
substrs[i][2] = str[i + 2];
substrs[i][3] = str[i + 3];
substrs[i][4] = 0;
}
// int n = strl(str) - 3;
info arr[n];//stores count of each different substrings
//sorting the substrings in lexicographical order
mergeSort(substrs, 0, n - 1);
int mx = 1;//Count of the most frequent substring
int p = 0;
for (int i = 0; i < n - 1;)
{
arr[p].count = 1;
int cnt = 1;
strcpy1(arr[p].string, substrs[i]);
if (i == n - 2 && lexi(substrs[i], substrs[i + 1]) != 0)
{
strcpy1(arr[p + 1].string, substrs[i + 1]);
arr[p + 1].count = 1;
p++;
}
if (lexi(substrs[i], substrs[i + 1]) == 0)
{
while (lexi(substrs[i], substrs[i + 1]) == 0 && i < n - 1)
{
arr[p].count++;
cnt++;
i++;
}
if (cnt > mx)
{
mx = cnt;
}
p++;
}
else
{
i++;
p++;
}
}
// printf("%d\n",mx);
for (int i = 0; i < p; i++)
{
if (arr[i].count == mx)
{
printf("%s\n", arr[i].string);
}
}
}