fork(2) download
  1. /*
  2. The idea here is that to get the overall maximum beauty of the string you must assign
  3. maximum beauty possible(26) to the most frequent alphabet and then 25 to the second most
  4. frequent alphabet and so on.
  5. And yes, in the code, sweetness and beauty means the same.
  6. */
  7.  
  8. #include<iostream>
  9. #include<cstdio>
  10. #include<algorithm>
  11. #include<cstring>
  12. #include<cstdlib>
  13. #include<cctype>
  14. #include<cmath>
  15. #include<climits>
  16. #include<vector>
  17. #include<iterator>
  18. #include<set>
  19.  
  20. #define fr(i,a,b) for(int i=a; i<b; i++)
  21. #define s(a) scanf("%d", &a)
  22. #define p(a) printf("%d\n", a)
  23. #define w(t) while(t--)
  24. #define pb push_back
  25. #define CLR(a) memset(a, 0, sizeof(a))
  26.  
  27. using namespace std;
  28.  
  29. typedef long long int lli;
  30. typedef vector<int> VI;
  31. typedef vector<string> VS;
  32.  
  33. int sweetness[26];
  34. pair<char, int> frequency[26];
  35.  
  36. bool cmp(pair<char, int> p, pair<char, int> q) {
  37. return p.second < q.second;
  38. }
  39.  
  40. int main() {
  41. int test;
  42. scanf("%d\n", &test);
  43. fr(t,1,test+1) {
  44. string s;
  45. getline(cin, s);
  46. string small;
  47. int len = s.length();
  48. CLR(frequency);
  49. CLR(sweetness);
  50. // Initialize frequency[].first with a, b, c, ..., z.
  51. fr(i,0,26)
  52. frequency[i].first = 97+i;
  53. fr(i,0,len) {
  54. /* as the beauty for both uppercase and lowercase characters are same
  55.   so lets first convert all characters to lowercase
  56.   */
  57. small[i] = tolower(s[i]);
  58. /* Sorry punctuation marks, you're not beautiful
  59.   as only alphabets have got some beauty, so
  60.   we skip punctuations and spaces
  61.   */
  62. if(small[i]==' '||ispunct(small[i]))
  63. continue;
  64. // and we increment the frequency of this particular alphabet
  65. frequency[small[i]-'a'].second++;
  66. }
  67. // Sort the frequency[] according to frequency[].second
  68. sort(frequency, frequency+26, cmp);
  69.  
  70. // Assign max sweetness to the most frequent alphabet and so on...
  71. for(int i=26; i>0; --i)
  72. sweetness[frequency[i-1].first - 'a'] = i;
  73. int ans = 0;
  74. fr(i,0,len) {
  75. if(small[i] == ' ' || ispunct(small[i]))
  76. continue;
  77. ans += sweetness[small[i]-'a'];
  78. }
  79. cout<<ans<<endl;
  80. }
  81. return 0;
  82. }
Success #stdin #stdout 0s 2732KB
stdin
Standard input is empty
stdout
Standard output is empty