-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathGame with String
55 lines (43 loc) · 884 Bytes
/
Game with String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t;
while(t--)
{
string s; cin>>s; int k; cin>>k;
int hash[26]; int n=s.length();
fill(hash,hash+26,0); int maxi=INT_MIN;
for(int i=0;i<n;i++)
{
hash[s[i]-'a']++;
maxi=max(maxi,hash[s[i]-'a']);
}
priority_queue<int>q;
for(int i=0;i<26;i++)
{
if(hash[i]!=0)
q.push(hash[i]);
}
while(k-- && !q.empty())
{
int val=q.top();
q.pop();
val=val-1;
if(val>0)
q.push(val);
}
if(q.empty())
cout<<0<<endl;
else
{
int ans=0;
while(!q.empty())
{
ans+=pow(q.top(),2);
q.pop();
}
cout<<ans<<endl;
}
}
return 0;
}