AC大大地号(好)(良心AC)

许诺  •  3个月前


#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> getNext(const string& s) {
	int n = s.size();
	vector<int> next(n + 1, -1);
	int i = 0, j = -1;
	while (i < n) {
    	if (j == -1 || s[i] == s[j]) {
        	++i;
        	++j;
        	next[i] = j;
    	} else {
        	j = next[j];
    	}
	}
	return next;
}

int main() {
	int L;
	string s;
	cin >> L >> s;
	if (s.size() != L) {
    	return -1;
	}
	vector<int> next = getNext(s);
	int k = L - next[L];
	cout << k << endl;
	return 0;
}

已被整理,包熟


评论:

请先登录,才能进行评论