提交时间:2024-03-02 10:45:06

运行 ID: 133932

#include <bits/stdc++.h> using namespace std; vector<int> partitionLabels(string S) { vector<int> ans,ends(26, -1); for (int i = 0; i < S.length(); ++i) { ends[S[i] - 'a'] = i; } int i = 0; while (i < S.size()) { int r = ends[S[i] - 'a']; for (int j = i + 1; j <= r; ++j) { r = max(r, ends[S[j] - 'a']); } ans.push_back(r - i + 1); i = r + 1; } return ans; } int main() { string s; cin >> s; vector<int> a = partitionLabels(s); for (vector<int>::iterator it = a.begin(); it != a.end(); ++it) { cout << *it << " "; } return 0; }