Skip to content

Commit 6919786

Browse files
committed
[Silver III] Title: 서로 다른 부분 문자열의 개수, Time: 1128 ms, Memory: 253696 KB -BaekjoonHub
1 parent 6170958 commit 6919786

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Silver III] 서로 다른 부분 문자열의 개수 - 11478
2+
3+
[문제 링크](https://www.acmicpc.net/problem/11478)
4+
5+
### 성능 요약
6+
7+
메모리: 253696 KB, 시간: 1128 ms
8+
9+
### 분류
10+
11+
자료 구조, 문자열, 집합과 맵, 해시를 사용한 집합과 맵, 트리를 사용한 집합과 맵
12+
13+
### 제출 일자
14+
15+
2026년 1월 4일 19:53:24
16+
17+
### 문제 설명
18+
19+
<p>문자열 S가 주어졌을 때, S의 서로 다른 부분 문자열의 개수를 구하는 프로그램을 작성하시오.</p>
20+
21+
<p>부분 문자열은 S에서 연속된 일부분을 말하며, 길이가 1보다 크거나 같아야 한다.</p>
22+
23+
<p>예를 들어, ababc의 부분 문자열은 a, b, a, b, c, ab, ba, ab, bc, aba, bab, abc, abab, babc, ababc가 있고, 서로 다른것의 개수는 12개이다.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 문자열 S가 주어진다. S는 알파벳 소문자로만 이루어져 있고, 길이는 1,000 이하이다.</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 S의 서로 다른 부분 문자열의 개수를 출력한다.</p>
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int n, answer;
6+
static char[] strs;
7+
static Set<String> set = new HashSet<>();
8+
static BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
9+
public static void main(String[] args) throws Exception{
10+
strs = br.readLine().toCharArray();
11+
n = strs.length;
12+
13+
for(int i = 1; i < n; i++){
14+
answer += recur(i);
15+
}
16+
17+
System.out.println(answer + 1);
18+
}
19+
20+
static int recur(int size){
21+
int s = 0;
22+
int e = 0;
23+
int ans = 0;
24+
25+
StringBuffer str = new StringBuffer();
26+
for(e = 0; e < size; e++){
27+
str.append(strs[e]);
28+
}
29+
30+
if(!set.contains(str.toString())){
31+
set.add(str.toString());
32+
ans++;
33+
}
34+
35+
for(; e < n; e++){
36+
str.deleteCharAt(0);
37+
str.append(strs[e]);
38+
39+
if(!set.contains(str.toString())){
40+
set.add(str.toString());
41+
ans++;
42+
}
43+
}
44+
return ans;
45+
}
46+
}

0 commit comments

Comments
 (0)