-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
82 lines (69 loc) · 2.21 KB
/
Solution.java
File metadata and controls
82 lines (69 loc) · 2.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package round1A.problem3;
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// t = number of testcases
int t = in.nextInt();
for (int i = 1; i <= t; ++i) {
int N = in.nextInt();
// Create a list of the reversed version of each word (ABC -> CBA)
ArrayList<String> words = new ArrayList<String>();
for(int j = 0; j < N; j++){
String word = in.next();
word = new StringBuilder(word).reverse().toString();
words.add(word);
}
Collections.sort(words);
// Now find the longest combinations of substrings
int r = findLongestStrings(words, true);
System.out.println("Case #" + i + ": " + r);
}
}
public static int findLongestStrings(ArrayList<String> list, boolean first){
int result = 0;
// Quick shortcut
if(list.size() < 2)
return 0;
else if(!first && list.size() == 2)
return 2;
// Check each character
char lastChar = 'A';
ArrayList<String> tmpList = new ArrayList<String>();
int emptyWords = 0;
for(int i = 0; i <= list.size(); i++){
// We loop one extra time to process the last character, so we set the word to a character which is not used.
String word;
if(i == list.size())
word = ".";
else
word = list.get(i);
if(word.charAt(0) == lastChar){
// Current first letter equals first letter of previous word, therefore we add this to the current word list.
if(word.length() == 1)
emptyWords++;
else
tmpList.add(word.substring(1));
}
else{
// First letter does not match first letter of last word, check if we have enough words to calculate something.
if((tmpList.size()+emptyWords) > 1){
int r = findLongestStrings(tmpList, false);
if(r <= ((tmpList.size()+emptyWords)-2))
r += 2;
result += r;
}
// Current char does not match previous char, so we reset some vars
emptyWords = 0;
lastChar = word.charAt(0);
tmpList = new ArrayList<String>();
if(word.length() == 1)
emptyWords++;
else
tmpList.add(word.substring(1));
}
}
return result;
}
}