Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created September 26, 2019 03:58
Show Gist options
  • Save surinoel/6ebefacf22d3ded02e822ef5d44e0e2a to your computer and use it in GitHub Desktop.
Save surinoel/6ebefacf22d3ded02e822ef5d44e0e2a to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
vector<int> makeTable(string pattern) {
int strsize = pattern.size();
vector<int> table(strsize);
int j = 0;
for(int i=1; i<strsize; i++) {
while(j > 0 && pattern[i] != pattern[j]) {
j = table[j - 1];
}
if(pattern[i] == pattern[j]) {
table[i] = ++j;
}
}
return table;
}
int main(void) {
ios_base::sync_with_stdio(false);
int n;
string pattern;
cin >> n >> pattern;
vector<int> table = makeTable(pattern);
cout << n - table[n - 1] << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment