Algorithm/카카오 기출
2021 KAKAO BLIND RECRUITMENT : 신규 아이디 추천(c++)
jungahshin
2021. 3. 8. 16:25
반응형
String으로 구현하면 되는 문제였다.
1~7단계까지 명시해준 조건의 내용을 그대로 구현해주면 되기 때문에 전혀 어려울 것이 없었다.
문자를 지우는 것은 모두 erase기능을 사용해서 지워주었다.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
string solution(string new_id) {
string answer = "";
// 1단계
string temp = new_id;
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
// 2단계
for(int i=0; i<temp.size(); i++){
if('a'<=temp[i] && temp[i]<='z') continue;
if(0<=(temp[i]-'0') && (temp[i]-'0')<=9) continue;
if(temp[i]=='-' || temp[i]=='_' || temp[i]=='.') continue;
temp.erase(temp.begin()+i);
i--;
}
// 3단계
bool check = false;
for(int i=0; i<temp.size(); i++){
if(temp[i]=='.'){
if(check==true){
temp.erase(temp.begin()+i);
i--;
}
check = true;
}else{
check = false;
}
}
// 4단계
if(temp.size()>0 && temp[0]=='.'){
temp.erase(temp.begin()+0);
}
if(temp.size()>0 && temp[temp.size()-1]=='.'){
temp.erase(temp.begin()+temp.size()-1);
}
// 5단계
if(temp==""){
temp = "a";
}
// 6단계
if(temp.size()>=16){
string newTemp = temp.substr(0, 15);
temp = newTemp;
}
if(temp[temp.size()-1]=='.'){
temp.erase(temp.begin()+temp.size()-1);
}
// 7단계
if(temp.size()<=2){
while(temp.size()!=3){
temp += (temp[temp.size()-1]);
}
}
answer = temp;
return answer;
}
|
cs |
반응형