Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + 10;
- vector<int> preprocess_lps(string pattern) {
- int j = 0, i = 1;
- vector<int> lps((int) pattern.size(), 0);
- lps[0] = 0;
- while(i < (int) pattern.size()) {
- if(pattern[i] == pattern[j]) {
- lps[i] = j + 1;
- j++;
- i++;
- }
- else {
- if(j != 0) {
- j = lps[j - 1];
- }
- else {
- lps[i] = 0;
- i++;
- }
- }
- }
- return lps;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- string s;
- cin >> s;
- vector<int> lps = preprocess_lps(s);
- int n = (int) s.size();
- vector<int> dp(n + 1, 0);
- for(int i = 0; i < n; i++) {
- dp[lps[i]]++;
- }
- for(int i = n - 1; i > 0; i--) {
- dp[lps[i - 1]] += dp[i];
- }
- for(int i = 0; i <= n; i++) {
- dp[i]++;
- }
- for(int i = 0; i < n; i++) {
- cout << lps[i] << " " << dp[lps[i]] << " " << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement