티스토리 뷰
반응형
신입사원 네오의 업무를 도와주기 위해
새로운 카카오 아이디를 만드려는 고객들이 규정에 맞지 않는 아이디를 입력한다면
규정에 맞는 새로운 아이디를 추천해주는 프로그램을 개발해야 하는 문제
문제의 조건
1. 고객이 입력한 아이디를 담은 string 배열 new_id가 주어짐
2. 아이디 변환 규칙
위 순서대로 차례차례 구현만 하면 된다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string new_id) {
string answer = new_id;
int stage = 1;
//5단계부터는 new_id가 빈 문자열이 될 수도 있기 때문에 처음 4단계까지만 반복문 속에서 반복
while (4 >= stage)
{
for (int i = 0; answer.length() > i; i++)
{
switch (stage)
{
case 1:
if ('A' <= answer[i] && 'Z' >= answer[i])
answer[i] = answer[i] - 'A' + 'a';
break;
case 2:
if ('a' <= answer[i] && 'z' >= answer[i])
continue;
else if ('0' <= answer[i] && '9' >= answer[i])
continue;
else if ('-' == answer[i])
continue;
else if ('_' == answer[i])
continue;
else if ('.' == answer[i])
continue;
else
{
//문자열이 줄어들었기 때문에 i번째 인덱스에 다른 문자가 위치하게 됨
//바뀐 i번째 문자도 탐색해야 하기 때문에 문자 삭제 후 i 감소 시켜줌
auto it = answer.begin();
advance(it, i);
answer.erase(it);
i--;
}
break;
case 3:
if (answer.length() - 1 == i)
continue;
if ('.' == answer[i] && '.' == answer[i+1])
{
auto it = answer.begin();
advance(it, i);
answer.erase(it);
i--;
}
break;
case 4:
if (0 == i || answer.length() - 1 == i)
{
if ('.' == answer[i])
{
auto it = answer.begin();
advance(it, i);
answer.erase(it);
}
}
break;
}
}
stage++;
}
//5단계부터는 따로 구현
if (answer.empty())
answer += 'a';
if (16 <= answer.length())
{
string tmp = "";
for (int i = 0; 15 > i; i++)
tmp += answer[i];
if ('.' == tmp[14])
{
auto it = tmp.begin();
advance(it, 14);
tmp.erase(it);
}
answer = tmp;
}
while (3 > answer.length())
{
char last = answer.back();
answer += last;
}
return answer;
}
반응형
'알고리즘 문제 풀이' 카테고리의 다른 글
[C++] 프로그래머스 순위 풀이(플로이드-워셜 알고리즘) (0) | 2021.10.19 |
---|---|
[C++] 프로그래머스 게임 맵 최단거리 풀이 (0) | 2021.10.19 |
[C++] 프로그래머스 로또의 최고 순위와 최저 순위 풀이 (0) | 2021.10.17 |
[C++] 프로그래머스 디스크 컨트롤러 풀이 (0) | 2021.10.16 |
[C++] 프로그래머스 단체사진 찍기 풀이 (0) | 2021.10.15 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 알고리즘
- greedy
- DFS
- 깊이우선탐색
- 해커랭크
- 캐나다
- 그리디
- 백준
- 캐나다생활
- hackerrank
- 하드웨어
- dp
- 프로그래머스
- C언어기초
- BFS
- 영어공부
- 프로그래밍
- 너비우선탐색
- 컴퓨터사이언스
- 컴퓨터
- 스위프트플레이그라운드
- 다이나믹프로그래밍
- 기초
- c++
- 컴퓨터공부
- 아이패드
- 애플
- c언어
- 문제풀이
- 코딩공부
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함