提交时间:2024-03-13 13:21:18
运行 ID: 138000
#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; }