import java.io.*;
import java.util.*;
public class lozinke {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] vals = new String[n];
for(int i = 0; i < n; i++) vals[i] = br.readLine();
Arrays.sort(vals, (a, b) -> a.length() - b.length());
HashMap<String, Integer> occ = new HashMap<String, Integer>();
int ans = 0;
for(int i = 0; i < n; i++) {
if(occ.containsKey(vals[i])) {
occ.put(vals[i], occ.get(vals[i]) + 1);
}
else {
occ.put(vals[i], 1);
}
}
for(String key: occ.keySet()) {
if(occ.get(key) > 1) {
ans += (occ.get(key) * (occ.get(key) - 1));
}
}
for(int i = 0; i < n; i++) {
HashSet<String> used = new HashSet<String>();
for(int j = 0; j < vals[i].length(); j++) {
for(int k = j; k < vals[i].length(); k++) {
String sub = vals[i].substring(j, k+1);
if(occ.containsKey(sub) && !used.contains(sub) && !(j == 0 && k == vals[i].length() - 1)) {
used.add(vals[i].substring(j, k+1));
ans += occ.get(vals[i].substring(j, k+1));
// System.out.println(sub + " " + vals[i]);
// occ.put(vals[i].substring(j, k+1), occ.get(vals[i].substring(j, k+1)) + 1);
}
}
}
}
PrintWriter pw = new PrintWriter(System.out);
pw.println(ans);
pw.close();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
9284 KB |
Output is correct |
2 |
Correct |
82 ms |
8688 KB |
Output is correct |
3 |
Correct |
79 ms |
9436 KB |
Output is correct |
4 |
Correct |
103 ms |
9396 KB |
Output is correct |
5 |
Correct |
179 ms |
14456 KB |
Output is correct |
6 |
Correct |
197 ms |
13388 KB |
Output is correct |
7 |
Correct |
202 ms |
14652 KB |
Output is correct |
8 |
Correct |
201 ms |
14196 KB |
Output is correct |
9 |
Correct |
456 ms |
17432 KB |
Output is correct |
10 |
Correct |
381 ms |
15240 KB |
Output is correct |
11 |
Correct |
555 ms |
17680 KB |
Output is correct |
12 |
Correct |
437 ms |
15728 KB |
Output is correct |
13 |
Correct |
545 ms |
16936 KB |
Output is correct |
14 |
Correct |
822 ms |
18220 KB |
Output is correct |
15 |
Correct |
563 ms |
17296 KB |
Output is correct |
16 |
Correct |
521 ms |
16236 KB |
Output is correct |
17 |
Correct |
302 ms |
15676 KB |
Output is correct |
18 |
Correct |
506 ms |
16412 KB |
Output is correct |
19 |
Correct |
642 ms |
17608 KB |
Output is correct |
20 |
Correct |
634 ms |
16752 KB |
Output is correct |