-
Notifications
You must be signed in to change notification settings - Fork 8
/
game-with-string.cpp
executable file
·60 lines (60 loc) · 1.31 KB
/
game-with-string.cpp
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
56
57
58
59
60
//
//game-with-string
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int main(){
int t,k;
cin>>t;
while(t--){
priority_queue<int> p;
string s;
cin>>s;
cin>>k;
int arr[26];
for(int i=0;i<26;i++) arr[i]=0;
for(int i=0;i<s.length();i++){
//cout<<(s[i]-48)<<" ";
arr[s[i]-97] +=1;
}
for(int i=0;i<26;i++){
if(arr[i] != 0){
p.push(arr[i]);
}
}
while(k--){
int x = p.top();
x-=1;
p.pop();
p.push(x);
}
int sum =0;
while(!p.empty()){
int y = p.top();
sum+= y*y;
p.pop();
}
//cout<<endl;
// sort(arr,arr+26);
// for(int i=0;i<26;i++)cout<<arr[i]<<" ";
// cout<<endl;
// int j=25;
// for(int i=0;i<k;i++){
// if(arr[j]>0 && arr[j]>=arr[j-1]){
// arr[j]-=1;
// }else{
// j--;
// if(j<1 && i<k){
// j=25;
// }
// }
// }
// for(int i=0;i<26;i++){
// sum += arr[i]*arr[i];
// }
cout<<sum<<endl;
}
return 0;
}