๐Ÿ“ Coding Test Study

C++ ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ

ibelieveinme 2021. 5. 20. 02:36
728x90

#1. ๋ฐ์ดํ„ฐ ํ˜•์‹์˜ ๊ตฌ๋ถ„

- string:

1) ์‚ฌ์šฉํ•˜๊ธฐ: string str = ""; (#include <string> ํ—ค๋”๋ฅผ ์จ์ค˜์•ผ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. "" ํฐ ๋”ฐ์˜ดํ‘œ ์‚ฌ์šฉ!)

2) ์ž…๋ ฅ๋ฐ›๊ธฐ: cin>>str; ๋กœ ํ•˜๊ณ  ๊ณต๋ฐฑ์ด ํฌํ•จ๋œ ๋ฌธ์ž์—ด์ด ์ €์žฅ๋œ๋‹ค.

3) ํฌ๊ธฐ ๊ตฌํ•˜๊ธฐ: str.size() ํ˜น์€ str.length(); ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

4) ์ถœ๋ ฅ: cout << str; ํ˜น์€ cout << str[i]๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด ํ•œ ๋ฌธ์ž์”ฉ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

- char:

1) ์‚ฌ์šฉํ•˜๊ธฐ: char charater[10]; ์ด๋ ‡๊ฒŒ ๋ฐฐ์—ดํ˜•์‹์œผ๋กœ ์จ์•ผ string ํ˜•์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ('' ์ž‘์€ ๋”ฐ์˜ดํ‘œ ์‚ฌ์šฉ!)

2) ์ž…๋ ฅ๋ฐ›๊ธฐ: cin>>charater; ํ•˜๋ฉด ํ•œ๊ธ€์ž์”ฉ charํ˜• ๋ฐฐ์—ด์— ์ €์žฅ๋œ๋‹ค. ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ณต๋ฐฑ ์ „๊นŒ์ง€ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๋‹ค. 

3) ํฌ๊ธฐ ๊ตฌํ•˜๊ธฐ: strlen(character); ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

4) ์ถœ๋ ฅ: cout << character;๋กœ ํ•œ๋ฒˆ์— ์ถœ๋ ฅ. ํ˜น์€ cout << character[i]๋กœ ํ•œ ๋ฌธ์ž์”ฉ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

#2. string ์œ ์šฉํ•จ์ˆ˜ ์ •๋ฆฌ

- tolower() : ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

1) ํ‘œ์ค€ํ˜•์‹: int tolower ( int c );

2) ์‚ฌ์šฉ์˜ˆ์ œ:

/* tolower example */
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main ()
{
  string str="Test String";
  for(int i = 0; i<str.length(); i++){
    str[i] = tolower(str[i]);
    cout<<str[i];
  }
  return 0;
}

 

- push_back(): string ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•˜๊ธฐ.(๋’ค์— ์‚ฝ์ž…)

1) ํ‘œ์ค€ํ˜•์‹: void push_back (char c);

2) ์‚ฌ์šฉ์˜ˆ์ œ: 

// string::push_back
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str = "ABC";
  str.push_back('D');
  cout << str << '\n'; // "ABCD"
  return 0;
}

 

- substr(): ํŠน์ • ๋ฌธ์ž์—ด ์ถ”์ถœ

1) ํ‘œ์ค€ํ˜•์‹: string substr (size_t pos = 0, size_t len = npos) const;

2) ์‚ฌ์šฉ์˜ˆ์‹œ:

// string::substr
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str="We think in generalities, but we live in details.";
  string str2 = str.substr (3,5);     // "think"
  size_t pos = str.find("live");      // position of "live" in str
  string str3 = str.substr (pos);     // get from "live" to the end
  cout << str2 << ' ' << str3 << '\n';// "think live in details."

  return 0;
}

 

# C++ ๋ ˆํผ๋Ÿฐ์Šค string

string - C++ Reference (cplusplus.com)

 

#์ฐธ๊ณ  ๋ฌธ์ œ

1) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ https://programmers.co.kr/learn/courses/30/lessons/72410

 

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

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

programmers.co.kr

์ •๋‹ต์ฝ”๋“œ๋Š” ๋”๋ณด๊ธฐ ํด๋ฆญ

๋”๋ณด๊ธฐ
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;

int main(){
    string id = "";
    cin >> id;
    cout << solution(id);
}

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

    //1๋‹จ๊ณ„: ๋Œ€๋ฌธ์ž->์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
    for(int i = 0; i<id.size(); i++){
        id[i] = tolower(id[i]);
    }
    //2๋‹จ๊ณ„: ~!@#$%^&*()=+[{]}:?,<>/ ๋ฌธ์ž ์ œ๊ฑฐ (ํ•ด๋‹น๋˜๋Š” ๋ฌธ์ž๋งŒ new_id[]์— ๋„ฃ๊ธฐ)
    for(int i = 0; i<id.size(); i++){
        if(id[i] != '-' && id[i] != '.' 
            && !('a'<= id[i] && id[i] <='z') 
            && !('0'<=id[i] && id[i]<='9')){
                continue;   
        }
        new_id.push_back(id[i]);
    }
    //3๋‹จ๊ณ„: ๋งˆ์นจํ‘œ(.)๊ฐ€ 2๋ฒˆ ์ด์ƒ ์—ฐ์†๋œ ๋ถ€๋ถ„์„ ํ•˜๋‚˜์˜ ๋งˆ์นจํ‘œ(.)๋กœ ์น˜ํ™˜
    string temp = "";
    for(int i = 0; i < new_id.size(); i++){
        if(new_id[i] == '.'){
            temp.push_back('.');
            while(i<new_id.size() && new_id[i] == '.') i++;
            i--;
        }
        else temp.push_back(new_id[i]);
    } 
    new_id = temp;

    //4๋‹จ๊ณ„: ์–‘๋์— ์žˆ๋Š” ๋งˆ์นจํ‘œ(.) ์ œ๊ฑฐ
    if(new_id[0]=='.') new_id = new_id.substr(1);
    if(new_id[new_id.size()-1]=='.') new_id = new_id.substr(0, new_id.size()-1);

    //5๋‹จ๊ณ„: id๊ฐ€ ๋นˆ๋ฌธ์ž์—ด์ผ ๊ฒฝ์šฐ, "a"๋ฅผ ๋Œ€์ž…
    if(new_id.empty()) new_id="a";

    //6๋‹จ๊ณ„: id ๊ธธ์ด๊ฐ€ 16์ž ์ด์ƒ์ด๋ฉด, 15๊ฐœ ์ดํ›„ ๋ฌธ์ž ๋ชจ๋‘ ์ œ๊ฑฐ
    if(new_id.size()>=16) new_id = new_id.substr(0,15);
    if(new_id[new_id.size()-1]=='.') new_id = new_id.substr(0, new_id.size()-1);

    //7๋‹จ๊ณ„: id ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ผ๋ฉด, id ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ id์˜ ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ๋์— ๋ถ™์ด๊ธฐ.
    while(new_id.size()<=2) new_id += new_id[new_id.size()-1];
  
    return new_id;
}

 

2) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๊ด„ํ˜ธ ํšŒ์ „ํ•˜๊ธฐ https://programmers.co.kr/learn/courses/30/lessons/76502

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ด„ํ˜ธ ํšŒ์ „ํ•˜๊ธฐ

 

programmers.co.kr

์ •๋‹ต์ฝ”๋“œ๋Š” ๋”๋ณด๊ธฐ ํด๋ฆญ

๋”๋ณด๊ธฐ
#include <string>
#include <stack>

using namespace std;

int findCorrectBracket(string s) {
	int correctBracketNum = 0;
	stack<char> s_openBracket;

	for (int idx = 0; idx < s.length(); idx++) {
		char closedBracket;
		char currentBracket = s[idx];
		//์—ด๋ฆฐ๊ด„ํ˜ธ
		if (currentBracket == '(' || currentBracket == '{' || currentBracket == '[') {
			s_openBracket.push(currentBracket);
		}
		//๋‹ซํžŒ๊ด„ํ˜ธ
		else {
			if (s_openBracket.empty()) {
				return 0;
			}
			switch (currentBracket) {
			case ')':
				if (s_openBracket.top() != '(') {
					return 0;
				}
				break;
			case '}':
				if (s_openBracket.top() != '{') {
					return 0;
				}
				break;
			case ']':
				if (s_openBracket.top() != '[') {
					return 0;
				}
				break;
			}
			s_openBracket.pop();
		}
	}
	if (s_openBracket.empty()) return 1;
    else return 0;
}

string rotateString(string s) {
	string firstString = s.substr(0, 1);
	string subString = s.substr(1, s.length() - 1);
	return subString + firstString;
}

int solution(string s) {
    int result = 0;
	for (int idx = 0; idx < s.length(); idx++) {
		result += findCorrectBracket(s);
		s = rotateString(s);
	}
	return result;
}

 

728x90