일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- lazy propagation
- 플로이드 와샬
- DP
- dfs
- 다익스트라
- Overfitting
- tensorflow
- 크루스칼
- 분할 정복
- pytorch
- 자바스크립트
- BFS
- object detection
- 너비 우선 탐색
- 알고리즘
- 이분 탐색
- 문자열
- 우선 순위 큐
- 세그먼트 트리
- 조합론
- 가끔은 말로
- 미래는_현재와_과거로
- back propagation
- 회고록
- 가끔은_말로
- NEXT
- 백트래킹
- dropout
- c++
- 2023
Archives
- Today
- Total
Doby's Lab
백준 1544번: 사이클 단어 (C++) 본문
https://www.acmicpc.net/problem/1544
Level: Silver IV
Solved By: Brute-Force, String
N의 최고 범위가 50, 문자열의 최대 길이가 50이라서 Brute-Force를 해도 되겠다는 생각이었습니다.
Brute-Force를 통해 비교를 할 때, 사이클 단어인지 판정하기 위해 string 헤더 파일의 substr 메서드를 활용했습니다.
두 단어가 사이클 단어라면, 비교한 단어 b를 a로 바꾸어 vector에 a를 할당하였습니다.
그리고, Coordinate Compression의 테크닉인 erase와 unique를 활용하여 중복된 값들을 전부 없애고, vector의 사이즈로 최종적인 답을 도출했습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int N;
vector<string> v;
bool check(string a, string b){
if(a.length() != b.length()) return false;
for(int i = 0; i < b.length(); i++){
string temp = b.substr(i, b.length() - i) + b.substr(0, i);
if(a == temp) return true;
}
return false;
}
int main(){
cin >> N;
for(int i = 0; i < N; i++){
string s; cin >> s; v.push_back(s);
}
for(int i = 0; i < v.size(); i++){
for(int j = i + 1; j < v.size(); j++){
if(i == j) continue;
// 같다면, 앞의 것과 똑같이 바꿀 것
if(check(v[i], v[j])){
v[j] = v[i];
}
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
cout << v.size();
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 1051번: 숫자 정사각형 (C++) (0) | 2023.03.01 |
---|---|
백준 1755번: 숫자놀이 (C++) (0) | 2023.03.01 |
백준 14397번: 해변 (C++) (0) | 2023.01.23 |
백준 2477번: 참외밭 (C++) (0) | 2023.01.08 |
백준 1004번: 어린 왕자 (C++) (0) | 2023.01.08 |