in a long string, find the longest substring which appears more than once, aka, longest repeated substring.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
// the idea is very neat. hard to think of
string longest_repeated_substring(string& s) {
if(s.empty()) return "";
vector<char*> vc;
//for(int i=0; i<s.size(); i++)
// vc.push_back(&s[i]);
for(char& c : s) // add pointers to each char to a vector
vc.push_back(&c);
sort(vc.begin(), vc.end(), [](char *a, char *b) { return *a < *b; });
int max_len = 0, start = 0;
char *last = &s[s.size()-1];
for(int i=0; i<vc.size()-1; i++) {
char *a = vc[i], *b = vc[i+1];
int len = 0;
while(a <= last && b <= last && *a == *b) { // allow overlap
// while(a <= last && b <= last && a < vc[i+1] && *a == *b) { // don't allow overlap
a++, b++;
len++;
if(max_len < len) {
max_len = len;
start = vc[i] - &s[0]; // gist, not i
}
}
}
if(max_len == 0) return "";
return s.substr(start, max_len);
}
int main()
{
string s = "bababa";
cout << longest_repeated_substring(s);
}