import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
int cases=Integer.parseInt(br.readLine());
for(int l=1;l<cases+1;l++){
StringTokenizer st=new StringTokenizer(br.readLine());
int x=Integer.parseInt(st.nextToken()),
y=Integer.parseInt(st.nextToken()),
c=Integer.parseInt(st.nextToken()),
o=Integer.parseInt(st.nextToken());
int []arr=new int[26];
for(int i=0;i<x;i++){
StringBuilder str=new StringBuilder(br.readLine());
for(int j=0;j<y;j++){
arr[str.charAt(j)-65]++;
}
}
Arrays.sort(arr);
int totalCost=arr[25]*c;
int counter=arr[25];
for(int j=24;j>-1;j--){
if(arr[j]!=arr[j+1]){
break;
}
totalCost+=arr[j]*c;
counter+=arr[j];
}
totalCost+=((x*y)-counter)*o;
String s=String.format("Case %d: %d\n",l,totalCost);
sb.append(s);
}
System.out.print(sb);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
int cases=Integer.parseInt(br.readLine());
for(int l=1;l<cases+1;l++){
StringTokenizer st=new StringTokenizer(br.readLine());
int x=Integer.parseInt(st.nextToken()),
y=Integer.parseInt(st.nextToken()),
c=Integer.parseInt(st.nextToken()),
o=Integer.parseInt(st.nextToken());
int []arr=new int[26];
for(int i=0;i<x;i++){
StringBuilder str=new StringBuilder(br.readLine());
for(int j=0;j<y;j++){
arr[str.charAt(j)-65]++;
}
}
Arrays.sort(arr);
int totalCost=arr[25]*c;
int counter=arr[25];
for(int j=24;j>-1;j--){
if(arr[j]!=arr[j+1]){
break;
}
totalCost+=arr[j]*c;
counter+=arr[j];
}
totalCost+=((x*y)-counter)*o;
String s=String.format("Case %d: %d\n",l,totalCost);
sb.append(s);
}
System.out.print(sb);
}
}
No comments:
Post a Comment