Header Ads Widget

Cool Name | codechef solution

 

Problem

Sardar Singh has many men fighting for him, and he would like to calculate the power of each of them to better plan for his fight against Ramadhir.

The power of a string  of lowercase English alphabets is defined to be

=1()

where () is the position of  in the alphabet, i.e, ()=1,()=2,,()=26.

Each of Sardar Singh's men has a name consisting of lowercase English alphabets. The power of a man is defined to be the maximum power over all possible rearrangements of this string.

Find the power of each of Sardar Singh's men.

Input Format

  • The first line of input contains an integer , denoting the total number of Sardar Singh's men.
  • Each of the next  lines contains a single string , the name of Sardar Singh's -th man.

Output Format

  • Output  lines, each containing a single integer. The -th of these lines should have the power of the -th of Sardar Singh's men.

Constraints

  • 160
  • 1100
  •  consists of lowercase english alphabets only.

Sample 1:

Input
Output
1
faizal
273

Explanation:

The rearrangement with maximum power is . Its power can be calculated as

1()+2()+3()+4()+5()+6()

which equals 273. It can be verified that no rearrangement gives a larger power.

Code(C++):-

#include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--)
    {
     string s;
     cin>>s;
     sort(s.begin(),s.end());
     int ans=0;
     for(int i=0;s[i];i++)
     {
     int a=s[i]-96;
     ans+=((i+1)*a);
     }
     cout<<ans<<endl;
    
    
    }
    return 0;
}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-





Post a Comment

0 Comments