코테

백준 단계별로 풀어보기 (5) - 문자열

hhzn 2024. 9. 9. 22:05

https://www.acmicpc.net/problem/27866

27866번 문자와 문자열

 

나의 풀이

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str;
	cin >> str;

	int a;
	cin >> a;
	cout << str[a-1];
}

단어와 정수가 주어졌을 때, 주어진 단어의 주어진 정수 번째 글자를 출력하는 프로그램이다.
string 객체는 [] operator 를 지원하기 때문에 string에 단어를 입력하고, []를 통해서 원하는 글자에 접근했다.


https://www.acmicpc.net/problem/2743

2743번 단어 길이 재기

 

나의 풀이

#include <iostream>
#include <string>

int main()
{
	std::string str;
	std::cin >> str;

	std::cout << str.length();
}

단어를 입력받아 그 길이를 출력하는 프로그램이다.

string 클래스의 멤버 함수인 length() 를 이용하였다.


https://www.acmicpc.net/problem/9086

9086번 문자열

 

나의 풀이

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

using namespace std;

int main()
{
	int t;
	cin >> t;

	for (int i = 0; i < t; ++i) {
		string str;
		cin >> str;
		cout << *str.begin() << *(str.end() - 1) << std::endl;
	}
}

문자열의 첫 글자와 마지막 글자를 출력하는 프로그램이다. 

반복자로 문자열의 첫 글자와 마지막 글자에 접근하여 그것이 가리키는 글자를 출력하도록 했다.

 


https://www.acmicpc.net/problem/11654

11654번 아스키 코드

 

나의 풀이

#include <iostream>

using namespace std;

int main()
{
	char c;
	cin >> c;
	cout << static_cast<int>(c);
}

https://www.acmicpc.net/problem/11720

11720번 숫자의 합

 

나의 풀이

#include <iostream>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;

	string str;
	cin >> str;
	int a = 0;
	for (int i = 0; i < str.length(); i++) {
		a += str[i] - '0';
	}
	cout << a;
}

공백 없이 쓰여있는 n개의 숫자를 모두 합해서 출력하는 프로그램이다.
첫째 줄에 숫자의 개수, 둘째 줄에 숫자들이 공백없이 주어진다.
모든 정수의 합을 구하기 위해서 문자열의 모든 글자에 '0' 을 뺄셈해 합을 구한다.

 


https://www.acmicpc.net/problem/10809

10809번 알파벳 찾기

 

나의 풀이

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	string str;
	cin >> str;

	for (char a = 'a'; a <= 'z'; ++a) {
		auto res = std::find(str.begin(), str.end(), a);
		if (res != str.end()) {
			cout << res - str.begin() << ' ';
		}
		else {
			cout << -1 << ' ';
		}
	}
}

문자열이 알파벳을 포함하는 경우에는 처음 등장하는 위치를 출력하는 프로그램이다. 

(그렇지 않은 경우에는 -1을 출력)

각각의 알파벳이 처음 등장하는 위치를 출력하기 위해서 알파벳의 수 만큼 for 루프를 돌도록 했다.
입력받은 단어에 해당 알파벳이 있는지 확인하는 find 함수를 사용해 있다면 반복자를 이용해서 위치를 출력하고,

find 함수가 end() 를 리턴했다면 -1을 출력한다.

 


https://www.acmicpc.net/problem/2675

2675번 문자열 반복

 

나의 풀이

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

using namespace std;

int main()
{
	int t;
	cin >> t;
	
	vector<pair<int, string>> v;
	v.reserve(1000);

	for (int i = 0; i < t; ++i) {
		int r;
		cin >> r;
		
		string str;
		cin >> str;
		
		v.emplace_back(make_pair(r, str));
	}

	for (pair<int, string> p : v) {
		for (int i = 0; i < p.second.length(); ++i) {
			for (int j = 0; j < p.first; ++j) {
				cout << p.second[i];
			}
		}
		cout << endl;
	}
}

반복 횟수와 단어를 한 쌍으로 취급하기 위해서 pair<int, string>의 vector를 선언했다.

벡터의 원소들을 range-for 를 이용해 모든 원소에 접근해서 글자를 반복해서 출력한다.


https://www.acmicpc.net/problem/1152

1152번 단어의 개수

 

나의 풀이

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

using namespace std;

int main()
{
	vector<string> v;
	v.reserve(10000);

	string str;
	while (cin >> str) {
		v.emplace_back(str);
	}

	cout << v.size();
}

string vector를 선언하고 문제에서 주어진 조건에 맞게 최대 단어 수 만큼 reverse를 한다.

입력을 받을 수 있을 때까지 원소를 vector에 추가하고 입력이 끝나면 vector의 size를 출력한다.


https://www.acmicpc.net/problem/2908

2908번 상수

 

나의 풀이

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	string s1, s2;
	cin >> s1 >> s2;

	reverse(s1.begin(), s1.end());
	reverse(s2.begin(), s2.end());
	int a = stoi(s1);
	int b = stoi(s2);

	if (a > b) {
		cout << a;
	}
	else {
		cout << b;
	}
}

reverse 함수를 이용해서 입력된 string의 순서를 역순으로 바꾼다.

입력으로 받은 두 수를 역순으로 바꾼 후, int 형 변수에 stoi 함수를 이용해서 정수 형태로 저장한다.

정수로 저장된 두 수를 비교해서 큰 수를 출력한다.


https://www.acmicpc.net/problem/5622

5622번 다이얼

 

나의 풀이

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	string str;
	cin >> str;
	
	vector<pair<int, vector<char>>> um;
	um.push_back(make_pair(2, vector<char>{'A', 'B', 'C'}));
	um.push_back(make_pair(3, vector<char>{'D', 'E', 'F'}));
	um.push_back(make_pair(4, vector<char>{'G', 'H', 'I'}));
	um.push_back(make_pair(5, vector<char>{'J', 'K', 'L'}));
	um.push_back(make_pair(6, vector<char>{'M', 'N', 'O'}));
	um.push_back(make_pair(7, vector<char>{'P', 'Q', 'R', 'S'}));
	um.push_back(make_pair(8, vector<char>{'T', 'U', 'V'}));
	um.push_back(make_pair(9, vector<char>{ 'W', 'X', 'Y', 'Z'}));

	int sum{ static_cast<int>(str.length()) };

	for (int i = 0; i < static_cast<int>(str.length()); ++i) {
		for (pair<int, vector<char>> elem : um) {
			auto res = find(elem.second.begin(), elem.second.end(), str[i]);
			if (res != elem.second.end())
				sum += elem.first;
		}
	}
	cout << sum;
}

숫자와 상응하는 알파벳들을 pair형태로 vector에 저장한다. 

해당하는 숫자에 1을 더한 시간이 걸리기 때문에 그 1을 더한다는 의미로 sum에 단어의 길이만큼을 초기 값으로 설정한다.

다음 입력으로 받은 알파벳들의 상응하는 수를 더해 최소 시간을 구한다.


https://www.acmicpc.net/problem/11718

11718번 그대로 출력하기

 

나의 풀이

#include <iostream>

using namespace std;

int main()
{
	char c;
	while (cin.get(c)) {
		cout << c;
	}
}

cin의 멤버함수 get()은 엔터 또한 입력받는 문자로 간주한다. 

공백이라도 get()을 이용해서 그대로 출력할 수 있다.


 

** 오류 지적은 환영입니다. ^^ **

'코테' 카테고리의 다른 글

백준 단계별로 풀어보기 (7)  (0) 2025.03.18
백준 단계별로 풀어보기 (6)  (1) 2024.09.25
백준 단계별로 풀어보기 (4)  (0) 2024.09.06
백준 단계별로 풀어보기 (3)  (6) 2024.09.02
백준 단계별로 풀어보기 (2)  (0) 2024.08.31