Skip to content

Commit 0fb6d6e

Browse files
authored
Merge pull request #100 from AlgorithmWithGod/khj20006
[20250212] BOJ / P3 / 별다줄 / 권혁준
2 parents ab58127 + 279a0bd commit 0fb6d6e

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Node{
7+
Node[] next;
8+
int cnt;
9+
Node(){
10+
next = new Node[26];
11+
cnt = 0;
12+
}
13+
}
14+
15+
class Trie{
16+
Node root;
17+
Trie(){
18+
root = new Node();
19+
}
20+
void insert(String str) {
21+
Node now = root;
22+
for(char i:str.toCharArray()) {
23+
if(now.next[i-'a'] == null) now.next[i-'a'] = new Node();
24+
now = now.next[i-'a'];
25+
now.cnt++;
26+
}
27+
}
28+
}
29+
30+
class Main {
31+
32+
// IO field
33+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
35+
static StringTokenizer st;
36+
37+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
38+
static int nextInt() {return Integer.parseInt(st.nextToken());}
39+
static long nextLong() {return Long.parseLong(st.nextToken());}
40+
static void bwEnd() throws Exception {bw.flush();bw.close();}
41+
42+
// Additional field
43+
static Trie trie;
44+
static int N;
45+
static long[][] count;
46+
static long[] dp;
47+
static long mod = (long)1e9 + 7;
48+
static String S;
49+
50+
public static void main(String[] args) throws Exception {
51+
52+
ready();
53+
solve();
54+
55+
bwEnd();
56+
}
57+
58+
static void ready() throws Exception{
59+
60+
trie = new Trie();
61+
N = Integer.parseInt(br.readLine());
62+
for(int i=0;i<N;i++) trie.insert(br.readLine());
63+
64+
S = br.readLine();
65+
dp = new long[S.length()+1];
66+
count = new long[S.length()+1][301];
67+
68+
}
69+
70+
static void solve() throws Exception{
71+
72+
for(int i=1;i<=S.length();i++) {
73+
74+
Node now = trie.root;
75+
// i로 시작하는 문자열, 길이 300까지만
76+
for(int j=1;i+j-2<S.length();j++) {
77+
// count[i+j-1][j]
78+
79+
int x = S.charAt(i+j-2)-'a';
80+
if(now.next[x] == null) break;
81+
now = now.next[x];
82+
count[i+j-1][j] = now.cnt;
83+
84+
}
85+
}
86+
87+
dp[0] = 1;
88+
for(int i=1;i<=S.length();i++) for(int j=1;j<=Math.min(300, i);j++) {
89+
dp[i] += (dp[i-j] * count[i][j]);
90+
dp[i] %= mod;
91+
}
92+
bw.write(dp[S.length()]+"\n");
93+
94+
}
95+
96+
97+
}
98+
99+
```

0 commit comments

Comments
 (0)