Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <unordered_map>
- #include <iomanip>
- #include <regex>
- #include <numeric>
- using namespace std;
- #define pii pair<int , int>
- #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
- const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
- const int MAX = 100005;
- string s;
- int arr[26];
- int used = 0;
- int consecutive[20][20];
- int dp[(1 << 20)];
- int main() {
- //freopen("talent.in", "r", stdin);
- //freopen("talent.out", "w", stdout);
- FAST;
- cin >> s;
- for(int i = 0; i < 26; i++){
- arr[i] = -1;
- }
- for(int i = 0; i < s.size(); i++) {
- if(i == 0){
- arr[s[0] - 'a'] = used;
- used++;
- }
- int nxt = i + 1;
- if (nxt >= s.size()) {
- continue;
- }
- if (arr[s[nxt] - 'a'] == -1) {
- arr[s[nxt] - 'a'] = used;
- used++;
- }
- consecutive[arr[s[i] - 'a']][arr[s[nxt] - 'a']]++;
- }
- memset(dp, 2e9, sizeof dp);
- dp[0] = 0;
- for(int state = 1; state < (1 << 20); state++){
- dp[state] = 2e9;
- for(int cur_char = 0; cur_char < 20; cur_char++){
- if(!(state & (1 << cur_char))){
- continue;
- }
- int val = dp[state ^ (1 << cur_char)];
- for(int added_char = 0; added_char < 20; added_char++){
- if(!(state & (1 << added_char))){
- continue;
- }
- val += consecutive[cur_char][added_char];
- }
- dp[state] = min(dp[state], val);
- }
- }
- cout << dp[(1 << 20) - 1] + 1<< "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement