728x90
320x100
11478번: 서로 다른 부분 문자열의 개수
첫째 줄에 문자열 S가 주어진다. S는 알파벳 소문자로만 이루어져 있고, 길이는 1,000 이하이다.
www.acmicpc.net
반응형
📌 접근 방법
⚡ ababc 입력
- i = 0일 때 : a, ab, aba, abab, ababc
- i = 1일 때 : b, ba, bab, babc
- i = 2일 때 : a, ab, abc
- i = 3일 때 : b, bc
- i = 4일 때 : c
➡ map 에 넣고 bool로 이미 있는 조합인지 아닌지 체크하며 카운팅
✅ Pass Code
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iterator>
#include<map>
#include<set>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
map<string, bool> m;
string str;
cin>>str;
int cnt=0;
for(int i=0; i<str.length(); i++){
string tmp="";
for(int j=i; j<str.length(); j++){
tmp+=str[j];
if(m[tmp]==false){
m[tmp]=true;
cnt++;
}
}
}
cout<<cnt<<"\n";
return 0;
}
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/002.gif)
728x90
반응형
'Coding Test > Baekjoon' 카테고리의 다른 글
[BOJ/백준/C++] 1934번 최소공배수 (0) | 2024.01.27 |
---|---|
[BOJ/백준/C++] 7785번 회사에 있는 사람 (0) | 2024.01.27 |
[BOJ/백준/C++] 18870번 좌표 압축 (0) | 2024.01.27 |
[BOJ/백준/C++] 1269번 대칭 차집합 (0) | 2024.01.27 |
[BOJ/백준/C++] 1181번 단어 정렬 (0) | 2024.01.27 |