Skip to content

Commit a428617

Browse files
authored
Update 1062-longest-repeating-substring.js
1 parent 83c62cb commit a428617

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

1062-longest-repeating-substring.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,65 @@ const longestRepeatingSubstring = function(s) {
5252

5353
// non-overlap version
5454
// http://nriverwang.blogspot.com/2013/04/longest-repeated-substring.html
55+
56+
/*
57+
You are to find the longest repeated substring in a given text.
58+
Repeated substrings may not overlap. If more than one substring is
59+
repeated with the same length, print the first one you find.(starting
60+
from the beginning of the text). NOTE: The substrings can't be all spaces.
61+
62+
Input Sample:
63+
Your program should accept as its first argument a path to a filename.
64+
The input file contains several lines. Each line is one test case.
65+
Each line contains a test string. eg.
66+
67+
banana
68+
abc
69+
70+
Output Sample:
71+
For each set of input produce a single line of output which is the
72+
longest repeated substring. If there is none, print out the string NONE. eg.
73+
an
74+
NONE
75+
76+
std::string repeated_substring(std::string &str) {
77+
int len = str.length();
78+
79+
int **c = new int*[len + 1];
80+
for (int i = 0; i <= len; ++i)
81+
c[i] = new int[len + 1];
82+
for (int i = 0; i <= len; ++i) {
83+
c[i][0] = 0;
84+
c[0][i] = 0;
85+
}
86+
87+
int max_len = 0, index = len + 1;
88+
for (int i = 1; i <= len; ++i) {
89+
for (int j = 1; j <= len; ++j) {
90+
if (str[i-1] == str[j-1] && abs(i-j) > c[i-1][j-1]) {
91+
c[i][j] = c[i-1][j-1] + 1;
92+
if (c[i][j] > max_len) {
93+
max_len = c[i][j];
94+
index = std::min(i, j);
95+
}
96+
} else {
97+
c[i][j] = 0;
98+
}
99+
}
100+
}
101+
102+
for (int i = 0; i <= len; ++i)
103+
delete[] c[i];
104+
delete[] c;
105+
106+
if (max_len > 0) {
107+
std::string ret = str.substr(index - max_len, max_len);
108+
for (int i = 0; i < max_len; ++i)
109+
if(ret[i] != ' ')
110+
return ret;
111+
}
112+
113+
return "NONE";
114+
}
115+
116+
*/

0 commit comments

Comments
 (0)