๋ฌธ์
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ์ ๊ท ์์ด๋ ์ถ์ฒ
์นด์นด์ค์ ์ ์ฌํ ์ ์ ๊ฐ๋ฐ์ ๋ค์ค๋ "์นด์นด์ค๊ณ์ ๊ฐ๋ฐํ"์ ๋ฐฐ์น๋์ด, ์นด์นด์ค ์๋น์ค์ ๊ฐ์ ํ๋ ์ ์ ๋ค์ ์์ด๋๋ฅผ ์์ฑํ๋ ์ ๋ฌด๋ฅผ ๋ด๋นํ๊ฒ ๋์์ต๋๋ค. "๋ค์ค"์๊ฒ ์ฃผ์ด์ง ์ฒซ ์ ๋ฌด๋ ์๋ก
programmers.co.kr
์ฝ๋
#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๋จ๊ณ์ธ ".."์ "."๋ก ์นํํ ๋ ์ ๊ท์์ ์ด์ฉํด์ ํ์์ต๋๋ค.
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด( Level 1, 2021 ์นด์นด์ค ์ธํด์ญ)
๋ฌธ์ ์ฝ๋ฉํ ์คํธ ์ฐ์ต - ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด ๋ค์ค์ ํ๋ก๋๊ฐ ์ซ์๋์ด๋ฅผ ํ๊ณ ์์ต๋๋ค. ๋ค์ค๊ฐ ํ๋ก๋์๊ฒ ์ซ์๋ฅผ ๊ฑด๋ฌ ๋ ์ผ๋ถ ์๋ฆฟ์๋ฅผ ์๋จ์ด๋ก ๋ฐ๊พผ ์นด๋๋ฅผ ๊ฑด๋ค์ฃผ๋ฉด ํ๋ก๋๋ ์๋ ์ซ
dkswnkk.tistory.com
ํนํ ์ ๋ฌธ์ ์ฒ๋ผ ๋์์ ๋ regex_replace๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ฑ ํ์ฉ๋ฉด์์ ๋ฐ์ด๋ฉ๋๋ค.
regex ํจ์๋ ์๋ ๊ฒ์๊ธ์์ ์ ๋ฆฌ ํ์ผ๋ ์ฐธ๊ณ ํ์๋ฉด ๋์์ด ๋์ค ๊ฒ ๊ฐ์ต๋๋ค.
[C++, ์ฝํ ์ฉ] ํน์ ๋ฌธ์ ์นํ regex_replace
ํน์ ๋ฌธ์ ๋ณํ regex_replace #include #include using namespace std; int main(){ string s = "OneTwoThree"; string s2 = "..."; //regex_replace (๋์, ์ ๊ทํํ์ ๊ฐ์ฒด, ์นํ ๊ฒฐ๊ณผ) s = regex_replace(s,..
dkswnkk.tistory.com
'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 |
๋๊ธ