提交时间:2023-11-19 13:42:42

运行 ID: 111240

#include <bits/stdc++.h> using namespace std; string S; vector<int> fg() { int hash[27] = {0}; // i为字符,hash[i]为字符出现的最后位置 for (int i = 0; i < S.size(); i++) { // 统计每一个字符最后出现的位置 hash[S[i] - 'a'] = i; } vector<int> result; int left = 0; int right = 0; for (int i = 0; i < S.size(); i++) { right = max(right, hash[S[i] - 'a']); // 找到字符出现的最远边界 if (i == right) { result.push_back(right - left + 1); left = i + 1; } } return result; } int main(){ cin>>S; vector<int> r = fg(); for(int i=0;i<r.size();i++) cout<<r[i]<<" "; return 0; }