Notice
Recent Posts
Recent Comments
Today
Total
01-27 17:46
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[2021 KAKAO BLIND RECRUITMENT] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[2021 KAKAO BLIND RECRUITMENT] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

WonderJay 2022. 6. 28. 11:52
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

์นด์นด์˜ค์— ์ž…์‚ฌํ•œ ์‹ ์ž… ๊ฐœ๋ฐœ์ž ๋„ค์˜ค๋Š” "์นด์นด์˜ค๊ณ„์ •๊ฐœ๋ฐœํŒ€"์— ๋ฐฐ์น˜๋˜์–ด, ์นด์นด์˜ค ์„œ๋น„์Šค์— ๊ฐ€์ž…ํ•˜๋Š” ์œ ์ €๋“ค์˜ ์•„์ด๋””๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—…๋ฌด๋ฅผ ๋‹ด๋‹นํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. "๋„ค์˜ค"์—๊ฒŒ ์ฃผ์–ด์ง„ ์ฒซ ์—…๋ฌด๋Š” ์ƒˆ๋กœ

programmers.co.kr

 

string ํด๋ž˜์Šค์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ž˜ ๋ชจ๋ฅด๋Š” ๋•์— ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ฝ”๋“œ๊ฐ€ ์ข€ ๋”๋Ÿฌ์›Œ์ง„ ๊ฒƒ ๊ฐ™๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, string::substring ์„ ์“ฐ๋ฉด ๊ฐ„๋‹จํ•œ๋ฐ, string::erase ๋ฅผ ์“ด๋‹ค๋˜๊ฐ€,

string::pop_back() ์ด ์žˆ๋Š” ์ค„ ๋ชฐ๋ž๋‹ค๋˜๊ฐ€..ใ…œใ…œ

string ํด๋ž˜์Šค๋ฅผ ์กฐ๋งŒ๊ฐ„ ์ •๋ฆฌํ•ด์•ผ๊ฒ ๋‹ค.

 

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

/*
1๋‹จ๊ณ„ new_id์˜ ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ๋Œ€์‘๋˜๋Š” ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
2๋‹จ๊ณ„ new_id์—์„œ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
3๋‹จ๊ณ„ new_id์—์„œ ๋งˆ์นจํ‘œ(.)๊ฐ€ 2๋ฒˆ ์ด์ƒ ์—ฐ์†๋œ ๋ถ€๋ถ„์„ ํ•˜๋‚˜์˜ ๋งˆ์นจํ‘œ(.)๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
4๋‹จ๊ณ„ new_id์—์„œ ๋งˆ์นจํ‘œ(.)๊ฐ€ ์ฒ˜์Œ์ด๋‚˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
5๋‹จ๊ณ„ new_id๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, new_id์— "a"๋ฅผ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.
6๋‹จ๊ณ„ new_id์˜ ๊ธธ์ด๊ฐ€ 16์ž ์ด์ƒ์ด๋ฉด, new_id์˜ ์ฒซ 15๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋“ค์„ ๋ชจ๋‘ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
     ๋งŒ์•ฝ ์ œ๊ฑฐ ํ›„ ๋งˆ์นจํ‘œ(.)๊ฐ€ new_id์˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ๋์— ์œ„์น˜ํ•œ ๋งˆ์นจํ‘œ(.) ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
7๋‹จ๊ณ„ new_id์˜ ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ผ๋ฉด, new_id์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ new_id์˜ ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ๋์— ๋ถ™์ž…๋‹ˆ๋‹ค.
*/

bool isdigit(char c)
{
    if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4'
        || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
        return true;
    else
        return false;
}

bool isspecial(char c)
{ 
    string special = "-_.~!@#$%^&*()=+[{]}:?,<>/";
    for (auto& ele : special)
    {
        if (c == ele)
            return true;
    }
    return false;
}

string solution(string new_id) {
    string answer = "";

    // 1๋‹จ๊ณ„ new_id์˜ ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ๋Œ€์‘๋˜๋Š” ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    for (auto& ele : new_id) // 1๋‹จ๊ณ„
    {
        // new_id ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ
        if (isupper(ele))
        {
            ele = tolower(ele);
        }
        answer += ele;
    }

    // 2๋‹จ๊ณ„ new_id์—์„œ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
    for (string::iterator iter = answer.begin(); iter != answer.end();iter++)
    {
        if (islower(*iter))
            continue;
        if (isdigit(*iter))
            continue;
        if (isspecial(*iter))
        {
            if (*iter == '-' || *iter == '_' || *iter == '.')
                continue;
            else
            {
                answer.erase(iter);
                if (answer.empty()) break;
                else
                    iter--;
                continue;
            }
        }
        
    }

    // 3๋‹จ๊ณ„
    if (!answer.empty())
    {
        for (string::iterator iter = answer.begin() + 1; iter != answer.end();)
        {
            if (*(iter - 1) == '.' && *iter == '.')
            {
                answer.erase(iter);
                continue;
            }
            iter++;
        }
        // 4๋‹จ๊ณ„
        if (answer.front() == '.')
            answer.erase(answer.begin());
        if (!answer.empty() && answer.back() == '.')
            answer.erase(answer.end() - 1);
    }
    //5๋‹จ๊ณ„
    if(answer.empty())
    {
        answer.append("a");
    }

    //6๋‹จ๊ณ„
    while (answer.length() >= 16)
    {
        answer.erase(answer.begin() + 15);
    }
    if (answer.back() == '.')
        answer.erase(answer.end()-1);
    
    //7๋‹จ๊ณ„ new_id์˜ ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ผ๋ฉด, new_id์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ new_id์˜ ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ๋์— ๋ถ™์ž…๋‹ˆ๋‹ค.
    if (answer.length() <= 2)
    {
  
        char last_ = answer.back();
        string last(1, last_);
        while (answer.length() != 3)
        {
            answer.append(last);
        }
    }
    return answer;
}

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments