思路:
不断贪心增加即可。
实现:
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 int ne[100005][26]; 5 int main() 6 { 7 string s; 8 while (cin >> s) 9 { 10 memset(ne, 0, sizeof ne); 11 int n = s.length(); 12 for (int i = 0; i < 26; i++) ne[n][i] = n; 13 for (int i = n - 1; i >= 0; i--) 14 { 15 for (int j = 0; j < 26; j++) 16 ne[i][j] = ne[i + 1][j]; 17 ne[i][s[i] - 'a'] = i; 18 } 19 int ans = 1; 20 for (int i = 1; i < n; i++) 21 { 22 int j = i, cur = 0; 23 while (j < n) 24 { 25 if (ne[cur][s[j] - 'a'] < i) 26 { 27 cur = ne[cur][s[j] - 'a']; 28 cur++; 29 j++; 30 } 31 else break; 32 } 33 ans++; 34 i = max(i, j - 1); 35 } 36 cout << ans << endl; 37 } 38 return 0; 39 }