第一个题解
2025-11-06 20:01:40
发布于:浙江
1阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> compute_prefix(const string& s) {
int n = s.size();
vector<int> pi(n, 0);
for (int i = 1; i < n; i) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j]) {
j = pi[j-1];
}
if (s[i] == s[j]) {
j;
}
pi[i] = j;
}
return pi;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int L;
string s;
cin >> L >> s;
vector<int> pi = compute_prefix(s);
cout << L - pi.back() << endl; // 使用back()获取最后一个元素,更简洁
return 0;
}
这里空空如也







有帮助,赞一个