fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4.  
  5. #undef int
  6. int main()
  7. {
  8. #define int long long int
  9.  
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0);
  12. cout.tie(0);
  13.  
  14. int t;
  15. cin>>t;
  16. while(t--)
  17. {
  18. string s;
  19. cin>>s;
  20. int n = s.length();
  21. vector<int> dp(n);
  22. vector< set<int> > sc(26);
  23. dp[0] = 0;
  24. sc[s[0]-'a'].insert(0);
  25. for(int i = 1;i<n;i++)
  26. {
  27. dp[i] = dp[i-1]+abs(s[i]-s[i-1]);
  28. if(!sc[s[i]-'a'].empty())
  29. dp[i] = min(dp[i], *(sc[s[i]-'a'].begin()));
  30. sc[s[i]-'a'].insert(dp[i]);
  31. }
  32. cout<<dp[n-1]<<endl;
  33. }
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty