๋ฌธ์
์ฝ๋
#include <string>
#include <vector>
#include <algorithm>
#include <regex>
using namespace std;
string solution(string new_id) {
string answer = "";
string id = new_id;
transform(id.begin(), id.end(), id.begin(), ::tolower); //1๋จ๊ณ
string temp="";
for(int i=0; i<id.length(); i++){ //2๋จ๊ณ
if(isalpha(id[i])||isdigit(id[i])||id[i]=='-'||id[i]=='_'||id[i]=='.') temp+=id[i];
}
id = temp;
temp.clear();
id = regex_replace(id, regex("[.]{2,}"),"."); //3๋จ๊ณ
if(id.back()=='.') id.pop_back(); //4๋จ๊ณ
if(id.front()=='.') id.erase(id.begin());
if(id.empty()) id+='a'; //5๋จ๊ณ
if(id.length()>=16){ //6๋จ๊ณ
while(id.length()!=15) id.pop_back();
if(id.back()=='.') id.pop_back();
}
if(id.length()<=2){ //7๋จ๊ณ
char last = id.back();
while(id.length()!=3) id+=last;
}
answer = id;
return answer;
}
ํ์ด(17๋ถ)
3๋จ๊ณ์ธ ".."์ "."๋ก ์นํํ ๋ ์ ๊ท์์ ์ด์ฉํด์ ํ์์ต๋๋ค.
ํนํ ์ ๋ฌธ์ ์ฒ๋ผ ๋์์ ๋ regex_replace๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ฑ ํ์ฉ๋ฉด์์ ๋ฐ์ด๋ฉ๋๋ค.
regex ํจ์๋ ์๋ ๊ฒ์๊ธ์์ ์ ๋ฆฌ ํ์ผ๋ ์ฐธ๊ณ ํ์๋ฉด ๋์์ด ๋์ค ๊ฒ ๊ฐ์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์คํ์ฑํ ๋ฐฉ( Level 2, 2019 ์นด์นด์ค ๋ธ๋ผ์ธ๋) (0) | 2021.10.22 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์์ฐ( Level 1) (0) | 2021.10.22 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์คํจ์จ( Level 1) (0) | 2021.10.22 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์์ ์ํธ( Level 1) (0) | 2021.10.21 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์คํฌํธ๋ฆฌ( Level 2) (0) | 2021.10.21 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด( Level 1, 2021 ์นด์นด์ค ์ธํด์ญ) (0) | 2021.10.21 |
๋๊ธ