YeJin's Footsteps

신규 아이디 추천 본문

Computer Science & Engineering/알고리즘

신규 아이디 추천

YeJinii 2021. 5. 2. 21:58

문제 링크

programmers.co.kr/learn/courses/30/lessons/72410

 

코딩테스트 연습 - 신규 아이디 추천

카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로

programmers.co.kr

 

문제 풀이 코드

#include <string>
#include <vector>
#include <iostream>
#include <cctype>

using namespace std;

string solution(string new_id) {
    
    //1단계
    for(int i=0; i<new_id.length(); i++){
        if(isupper(new_id[i])){
            new_id[i]=tolower(new_id[i]);
        }
    }
    
    //2단계
    string tmp="";
    for(int i=0; i<new_id.length(); i++){
        if(!isdigit(new_id[i])&&!isalpha(new_id[i])){
            if(new_id[i]=='.'||new_id[i]=='-'||new_id[i]=='_') tmp+=new_id[i];
            else{
                continue;
            }
        }
        else{tmp+=new_id[i];}
    }
    new_id=tmp; tmp="";
    
    //3단계
    int cntp=0;
    for(int i=0; i<new_id.length(); i++){
        if(new_id[i]=='.') {
            cntp++;
            if(cntp>=2){
                continue;
            }
            else{
                tmp+=new_id[i];
            }
        }
        else{
            cntp=0;
            tmp+=new_id[i];
        }        
    }
    new_id=tmp; tmp="";
    
    //4단계
    if(new_id[0]=='.') new_id.erase(0,1);
    if(new_id[new_id.size()-1]=='.') new_id.erase(new_id.size()-1,1); 
    
    //5단계
    if(new_id.size()==0){
        new_id="a";
    }
    
    //6단계
    if(new_id.size()>=16){
        for(int i=0; i<15; i++){
            tmp+=new_id[i];
        }
        new_id=tmp;
    }   
    if(new_id[new_id.size()-1]=='.') new_id.erase(new_id.size()-1,1);
    
    //7단계
    if(new_id.size()<=2){
        char lastc=new_id[new_id.size()-1];
        while(new_id.size()<3){
            new_id+=lastc;
        }
    }
    
    string answer = new_id;
    return answer;
}

'Computer Science & Engineering > 알고리즘' 카테고리의 다른 글

가장 큰 수  (0) 2021.07.01
불량사용자  (0) 2021.05.07
튜플  (0) 2021.05.02
크레인 인형 뽑기 게임  (0) 2021.05.02
완주하지 못한 선수  (0) 2021.05.02
Comments