Skip to content

Commit 7e798b2

Browse files
authored
Kattis solutions
1 parent 0e87f66 commit 7e798b2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.*;
2+
3+
/**
4+
* Created by AMK on 7/30/2019.
5+
* Life is nice :)
6+
* Enjoy coding :D
7+
*/
8+
public class Main2 {
9+
private static HashMap<String, Integer> table = new HashMap<>();
10+
private static HashMap<String, Long> memo = new HashMap<>();
11+
private static ArrayList<String> keys = new ArrayList<>();
12+
13+
public static void main(String[] args) {
14+
Scanner scanner = new Scanner(System.in);
15+
int N = scanner.nextInt();
16+
String W = scanner.next();
17+
for (int i = 0; i < N; i++) {
18+
String str = scanner.next();
19+
int val = scanner.nextInt();
20+
keys.add(str);
21+
table.put(str, val);
22+
}
23+
System.out.println(Calc(W));
24+
}
25+
private static long Calc(String string){
26+
if (memo.containsKey(string)) {
27+
return memo.get(string);
28+
}
29+
if (string.length() == 0){
30+
return 0;
31+
}
32+
long mark = table.containsKey(string) ? table.get(string) : 0;
33+
int INF = 1000000007;
34+
for (String key : keys) {
35+
if (string.startsWith(key)) {
36+
mark += (table.get(key) * Calc(string.substring(key.length(), string.length())) % INF);
37+
}
38+
}
39+
memo.put(string,mark % INF);
40+
return mark % INF;
41+
}
42+
}

0 commit comments

Comments
 (0)