-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMain2.java
More file actions
65 lines (57 loc) · 1.38 KB
/
Main2.java
File metadata and controls
65 lines (57 loc) · 1.38 KB
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
61
62
63
64
65
import java.util.*;
public class Main2 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int T;
int[] res;
String temp;
T = Integer.parseInt(cin.nextLine());
if (T <= 0 || T > 1000)
return;
res = new int[T];
for (int i = 0; i < T; i++) {
temp = cin.nextLine();
temp = toLowercase(temp);
res[i] = getWeight(temp);
}
for (int i = 0; i < T; i++)
System.out.println(res[i]);
}
private static int getWeight(String temp) {
int res = 0;
char pre = 0;
int count = 0;
for (int j = 0; j < temp.length(); j++) {
char cur = temp.charAt(j);
if (cur != pre) {
//System.out.println("!=");
res += getWeight(pre) * count * count;
pre = cur;
count = 1;
} else {
//System.out.println("=");
count++;
}
}
//System.out.println("inner :" + count);
res += getWeight(pre) * count * count;
return res;
}
private static int getWeight(char x) {
return x-'a'+1;
}
private static String toLowercase(String temp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < temp.length(); i++)
sb.append(toLowercase(temp.charAt(i)));
return sb.toString();
}
private static String toLowercase(char temp) {
if (temp <= 'z' && temp >= 'a')
return temp + "";
else if (temp >= 'A' && temp <= 'Z')
return (char) ('a' + temp - 'A') + "" + (char) ('a' + temp - 'A');
else
return "";
}
}