Skip to content

Instantly share code, notes, and snippets.

@zzt93
Created August 14, 2016 09:03
Show Gist options
  • Save zzt93/8336e8cdd9092b42bfee513a78b98e7c to your computer and use it in GitHub Desktop.
Save zzt93/8336e8cdd9092b42bfee513a78b98e7c to your computer and use it in GitHub Desktop.
vector<int> getNext(const string& s) {
vector<int> res(s.length());
res[0] = -1;
const char* start = s.c_str();
int j = -1;
for(int i = 0; i < s.length() - 1; ) {
if(j == -1) {
res[i + 1] = 0;
i++;
j = 0;
continue;
}
if(start[i] == start[j]) {
res[i + 1] = j + 1;
j++;
i++;
} else {
j = res[j];
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment