English
[BOJ 4821, CPP] Page counting

[BOJ 4821, CPP] Page counting

C++ Solution to BOJ 4821, "Page Counting" Problem

Problem link

boj 4821

Classification

Implementations, Strings, Parsing

Include

I recently joined a new company, and it's been quite a busy time. I'm a new employee who feels a lot of pressure just being in the company... However, I've been writing down various stories, so I'm sure I'll be able to tell them sooner or later.

Recently, I had a great opportunity to learn C, and I'm always disappointed when I hit a time wall with Python or Java, so I wanted to take this opportunity to solve problems in C++.

From my short experience with it, I have a pretty good feeling that the implementation is quite cumbersome, but the speed compensates for it. If I can master it, will I be able to take advantage of the speed...? I'm looking forward to it.

Description.

  • Vector: dynamic array structure, similar to an array, but with the big advantage that the size can be flexibly changed and memory allocation is automatic!

  • npos: no position, an index value to check when the position is invalid.

  • stringstream: a device for treating strings as streams, allowing them to be treated in the same way as input and output, often used for parsing

The basic logic is to create a bool array to cover the entire range of pages, then check for all the pages that came in and print the count at the end.

However, it's worth noting that the range of numbers given in the problem can be given as values beyond int, so you'll need to set a variable to control that, and there are exceptions to not check if the range is given in reverse order.

This was fairly easy to do in Python, but in C++, the code became very long...

Solution code

// 페이지 세기

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int				total_page, seperator, start, end, result;
	string			temp, each_range;
	vector<string>	line_info;
	vector<bool>	print_info;
	vector<int>		result_vector;

	while (true)
	{
		cin >> total_page;

		if (!total_page)
			break ;
		else
		{
			print_info.assign(1001, false);
			cin.ignore();
			getline(cin, temp);
			stringstream range_stream(temp);

			while (getline(range_stream, each_range, ','))
			{
				if (each_range.find('-') == string::npos)
				{
					if (each_range.length() < 5 && stoi(each_range) <= 1000)
						print_info[stoi(each_range)] = true;
				}
				else
				{
					seperator = each_range.find('-');
					if (each_range.substr(0, seperator).length() < 5
						&& stoi(each_range.substr(0, seperator)) <= 1000)
						start = stoi(each_range.substr(0, seperator));
					else
						start = 1001;
					if (each_range.substr(seperator + 1, each_range.size()).length() < 5 
						&& stoi(each_range.substr(seperator + 1, each_range.size())) <= 1000)
						end = stoi(each_range.substr(seperator + 1, each_range.size()));
					else
						end = 1001;
					if (start <= end)
					{
						for (int i = start; i <= end; i++)
						{
							if (i <= total_page)
								print_info[i] = true;
						}
					}
				}
			}

			result = 0;
			for (int i = 1; i <= total_page; i++)
			{
				if (print_info[i])
					result++;
			}
			result_vector.push_back(result);
		}
	}

	for (int i = 0; i < (int) result_vector.size(); i++)
		cout << result_vector[i] << endl;

	return (0);
}

댓글 작성

게시글에 대한 의견을 남겨 주세요.

댓글 0