Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9-dhlee777 #42

Merged
merged 1 commit into from
May 19, 2024
Merged

9-dhlee777 #42

merged 1 commit into from
May 19, 2024

Conversation

dhlee777
Copy link
Contributor

@dhlee777 dhlee777 commented May 5, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

AC

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

4์‹œ๊ฐ„30๋ถ„

๋ฌธ์ž์—ด์ฒ˜๋ฆฌ

์ด ๋ฌธ์ œ์—์„œ ๋ฐฐ์—ด์˜ ์›์†Œ๋“ค์ด [1,2,3,5] ์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ ์ผ๋‹จ์ด ๋ฐฐ์—ด์„ string ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ
์ด ๋ฌธ์ž์—ด์—์„œ ์ˆซ์ž๋“ค์„ ๋ถ„๋ฆฌํ•ด ๋‚ด๊ธฐ ์œ„ํ•ด์„œ for๋ฃจํ”„๋ฌธ์„ string ์‚ฌ์ด์ฆˆ ๋งŒํผ ๋Œ๋ฆฌ๋ฉฐ ํ™•์ธํ•ด์ค€๋‹ค. ์ด๋•Œ ์ˆซ์ž๊ฐ€ 10์˜์ž๋ฆฌ ์ด์ƒ ์ผ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๊ทธ ์ˆ˜๋“ค์„ ์ €์žฅํ•ด๋†“์„ string๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋” ์ƒ์„ฑํ•ด์ค€๋‹ค.
1."[","]"์˜ ๊ฒฝ์šฐ ๊ฑด๋„ˆ๋›ด๋‹ค.
2.","๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ ๋ฌธ์ž๋“ค์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ string ๊ฐ์ฒด์— ๊ณ„์†ํ•ฉ์ณ์ค€๋‹ค.
3. ","๊ฐ€ ๋‚˜์˜ฌ๊ฒฝ์šฐ ๊ทธ์ „๊นŒ์ง€ ํ•ฉ์ณ์ง„ string ๋ฌธ์ž์—ด๋“ค์„ stoi()๋ฅผ ํ†ตํ•ด ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฒกํ„ฐ์— pushํ•ด์ค€๋‹ค.
4. ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋Š” , ํ‘œ์‹œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ๋”ฐ๋กœ ๋ฒกํ„ฐ์— pushํ•ด์ค€๋‹ค.

์ฒซ๋ฒˆ์งธํ’€์ด(์‹œ๊ฐ„์ดˆ๊ณผ)

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<int>v;
string a,b,nm;
int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t,num;
	bool flag = true;
	cin >> t;
	while (t--) {
		flag = true;
		cin >> a>>num>>b;
		for (int i = 0; i <b.size(); i++) {
			if (b[i] == '[' || b[i] == ']')
				continue;
			else if (b[i] == ',')
			{
				v.push_back(stoi(nm));
				nm.clear();
			}
			else 
				nm = nm + b[i];
				
		}
		if(!nm.empty()) v.push_back(stoi(nm));
		nm.clear();

		for (int i = 0; i < a.size(); i++) {
			if (a[i] == 'R')
				reverse(v.begin(), v.end());
			else if (a[i] = 'D')
				if (v.empty())
				{
					cout << "error"<<"\n";
					flag = false;
					break;
				}
				else v.erase(v.begin());

		}
		if (!flag) continue;
		cout << "[";
		for (int j = 0; j < v.size(); j++) {
			if (j == v.size() - 1) cout << v[j];
			else cout << v[j] << ",";
		}
		cout << "]";
		cout << "\n";
		vector<int>().swap(v);

	}
}

์ฒซ๋ฒˆ์งธํ’€์ด์—์„œ Rํ•จ์ˆ˜ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด stl์˜ reverseํ•จ์ˆ˜๋ฅผ ์“ฐ๊ณ  Dํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ์œ„ํ•ด erase ํ•จ์ˆ˜๋ฅผ ์จ๋ฒ„๋ ธ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค. ๋‹น์—ฐํ•œ๊ฒƒ์ด ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์‹ญ๋งŒ๊ฐœ๊ฐ€ ๋˜๋Š”๋ฐ ์ด๋Ÿฌํ•œ reverse ,erase ํ•จ์ˆ˜๋ฅผ ์จ๋ฒ„๋ฆฌ๋ฉด ์“ธ๋•Œ๋งˆ๋‹ค ๋ชจ๋“  ์›์†Œ๋“ค์ด ์ž๋ฆฌ์ด๋™์„ ํ•˜๊ฒŒ๋˜๋ฏ€๋กœ ์—„์ฒญ๋‚œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ฐœ์ƒํ• ๊ฒƒ์ด๋‹ค.

๋‘๋ฒˆ์งธํ’€์ด

์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•ด์ค„๊ฒŒ ๋„ˆ๋ฌด๋งŽ๊ณ  ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ์— ์‹œ๊ฐ„์ด ์—„์ฒญ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.R์€ ๋ฐฐ์—ด์— ์žˆ๋Š” ์ˆ˜๋“ค์˜ ์ˆœ์„œ๋ฅผ ์—ญ์œผ๋กœ ๋’ค์ง‘๋Š” ํ•จ์ˆ˜ D๋Š” ์ฒซ๋ฒˆ์งธ์ˆ˜๋ฅผ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ฃผ์˜ํ• ์ ์€ ๋ฐฐ์—ด์— ๋น„์–ด์žˆ๋Š”๋ฐ D๋ฅผ ํ• ๊ฒฝ์šฐ error๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค.๋˜ํ•œ ์ถœ๋ ฅ์œผ๋กœ ๋นˆ๋ฐฐ์—ด์ด ๋‚˜์˜ฌ๊ฒฝ์šฐ []ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ์„ํ•ด์ค˜์•ผํ•œ๋‹ค.
๊ทธ๋ž˜์„œ ํžŒํŠธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ’€์–ด๋‚ธ ๋‘๋ฒˆ์งธ ํ’€์ด๋Š” ๋ฒกํ„ฐ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋‘๊ฐœ์˜ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•œ ํ’€์ด์ด๋‹ค. start_idx๋Š” ๋ฒกํ„ฐ์˜ ์ฒซ๋ฒˆ์งธ์›์†Œ, end_idx ๋Š” ๋ฒกํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง€๊ฒŒ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚œํ›„
1)R์—ฐ์‚ฐ์ด ๋ฐœ์ƒํ• ๊ฒฝ์šฐ
์›๋ž˜๋Š” start_idx์™€ end_idx์˜ ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊ฟ”์•ผํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์„ ์ค„์ด๊ธฐ์œ„ํ•ด ์ผ๋‹จ r_cnt๋ฅผ ํ†ตํ•ด R์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•ด์ค€๋‹ค.
2) D์—ฐ์‚ฐ์ด ๋ฐœ์ƒํ• ๊ฒฝ์šฐ์—๋Š”
๋งŒ์•ฝ ๋นˆ๋ฐฐ์—ด์ธ๊ฒฝ์šฐ ์‚ญ์ œํ• ๊ฒŒ ์—†์œผ๋ฏ€๋กœ ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋‹ค์Œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
๋งŒ์•ฝ RRRD์ฒ˜๋Ÿผ R์—ฐ์‚ฐ์ด ํ™€์ˆ˜๋ฒˆ ๋ฐœ์ƒํ•˜์˜€์„๊ฒฝ์šฐ R์—ฐ์‚ฐ(start_idx์™€ end_idx ์œ„์น˜๋ฐ”๊พธ๊ธฐ)๋ฅผ ํ•œ๋ฒˆ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋„˜์–ด๊ฐ„๋‹ค.(R์—ฐ์‚ฐ์ด
์ง์ˆ˜๋ฒˆ ๋ฐœ์ƒํ•œ๊ฒฝ์šฐ๋Š” ์›์ƒํƒœ๋กœ ํšŒ๊ท€ํ•˜๊ธฐ๋•Œ๋ฌธ์— ์›๋ณธ์ƒํƒœ ๊ทธ๋Œ€๋กœ ๋‹ค์Œ๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐ„๋‹ค)
if(start_idx>end_idx)์˜ ๊ฒฝ์šฐ ์‹œ์ž‘์ธ๋ฑ์Šค๊ฐ€ ๋” ๋’ค์—์žˆ์œผ๋ฏ€๋กœ start_idx--๋ฅผ ํ•˜์—ฌ ์‹œ์ž‘์ธ๋ฑ์Šค ์œ„์น˜ ์ด๋™
else if(start_idx<end_idx)์˜ ๊ฒฝ์šฐ ์ผ๋ฐ˜์ ์ธ๊ฒฝ์šฐ๋กœ ์‹œ์ž‘์ธ๋ฑ์Šค๊ฐ€ ์•ž์—์žˆ์œผ๋ฏ€๋กœ stat_idx++๋ฅผ ํ•˜์—ฌ ์‹œ์ž‘์ธ๋ฑ์Šค ์œ„์น˜ ์ด๋™
else(start_idx==end_idx) ๋งŒ์•ฝ ๋‘์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์€๊ฒฝ์šฐ์—๋Š” ๋ฒกํ„ฐ์˜ ์›์†Œ๊ฐ€ ํ•˜๋‚˜์ธ ์ƒํƒœ์ด๋ฏ€๋กœ D์—ฐ์‚ฐ์„ ํ•ด์ค„๊ฒฝ์šฐ ๋นˆ๋ฒกํ„ฐ๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค ์ด๋ฅผ ํ‘œํ˜„ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด end_idx=-1๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

์—ฌ๊ธฐ์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ์ค„์ผ๋ฐฉ๋ฒ•์€ RRRRD ์ด๋Ÿฐ์‹์œผ๋กœ R์ด ์ง์ˆ˜๊ฐœ๊ฐ€ ๋‚˜์™€๋ฒ„๋ฆฌ๋ฉด ๊ฒฐ๊ตญ ์›์ƒํƒœ๋กœ ํšŒ๊ท€ํ•˜๋ฏ€๋กœ R์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค ์ฆ‰ R์˜๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜๊ฐœ ๋‚˜์˜ฌ๋•Œ๋งŒ R์—ฐ์‚ฐ์„ ํ•œ๋ฒˆ ์‹œํ–‰ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ฒ˜์Œ์— ํ’€๋•Œ stl ๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์—ˆ์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๋งŒ ๊ณ„์†๋‚ฌ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ด๋ ‡๊ฒŒ ๋ฐฐ์—ด์›์†Œ๋ฅผ ์ด๋™์‹œํ‚ค์ง€ ์•Š๊ณ  ๋ฐฐ์—ด์˜ ์›์†Œ๊ฐ€ ์ด๋™ํ•œ๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ด๋Š”๊ฒŒ ๋˜๊ฒŒ ์‹ ๊ธฐํ•œ ํ’€์ด๋ฐฉ๋ฒ• ๊ฐ™์•˜๋‹ค.

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งž์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐฐ์—ด์„ ๋’ค์ง‘๊ณ ํ•˜๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ O(N)์ด N๋ฒˆ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๊ณ  t๋„ ๋„ฃ๊ฒŒ ๋˜๋ฉด O(N^3)์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํˆฌ ํฌ์ธํ„ฐ๋กœ ์ฒ˜์Œ๊ณผ ๋’ค์— ์ธ๋ฑ์Šค๋ฅผ ๋จผ์ € ์„ ์ •ํ•œ๋’ค ๋’ค์ง‘์—ˆ๋Š”์ง€ ํŒ๋ณ„ ์—ฌ๋ถ€๋งŒ ํ™•์ธํ•˜์—ฌ ์ธ๋ฑ์Šค๋ฅผ ์ค„์—ฌ์ค€๋‹ค๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ž˜ ์ง‘์€ ๊ฒƒ ๊ฐ™๋„ค์š”
์•„๋ž˜๋Š” ์ œ ํ’€์ด ์ž…๋‹ˆ๋‹ค.

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

using namespace std;

int T, n, front, last, i, j;
string p, x, num;
vector<int> v;
bool removeFront, pass;

void Solve()
{
	cin >> T;

	while (T--)
	{
		v.clear();
		num = "";
		removeFront = true;
		
		cin >> p >> n >> x;
		
		for (i = 0; i < x.length(); i++)
		{
			if (x[i] >= '0' && x[i] <= '9')
			{
				num += x[i];
			}
			else if (x[i] == ',' || x[i] == ']' && num != "")
			{
				v.push_back(stoi(num));
				num = "";
			}
		}

		pass = true;
		front = 0, last = v.size();

		for (i = 0; i < p.length(); i++)
		{
			if (p[i] == 'R')
			{
				removeFront = (removeFront ? false : true);
			}
			else
			{
				if (front == last)
				{
					cout << "error" << "\n";
					pass = false;
					break;
				}
				if (removeFront) front++;
				else last--;
			}
		}

		if (pass)
		{
			vector<int> ans;
			int len = last - front;

			cout << "[";
			for (i = 0; i < len; i++)
			{
				cout << (removeFront ? v[front++] : v[--last]);				
				if (i != len-1) cout << ",";
			}
			cout << "]\n";
		}
	}
}

int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Solve();

	return 0;
}

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•˜๋Š” ๋ถ€๋ถ„ ์ธ์ƒ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค.
์ €๋Š” ์ด๋ ‡๊ฒŒ ํ•œ ๋ฒˆ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

import sys
from collections import deque

input = sys.stdin.readline

T = int(input())
for i in range(T):
  hi = True
  er = 'y'
  a = list(input().strip())
  n = int(input())
  s = input().strip()[1:-1]
  if not s:
    if 'D' in a:
      er = 'error'
    else:
      er = 'y'
  else:
    s = deque((map(int,s.split(','))))
    for j in range(len(a)):
      if a[j]=='R':
        if hi:
          hi = False
        else:
          hi = True
      else:
        if not s:
          er = 'error'
        else:
          if hi:
            s.popleft()
          else:
            s.pop()

    if hi == False:
      s.reverse()
    else:
      s = s
  if er=='error':
    print(er)
  else:
    s = list(s)
    re = str(s).replace(" ", "")
    print(re)

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋Ÿฐ ๊ตฌํ˜„ ๋ฌธ์ œ๋Š” ํ•ญ์ƒ ์ „์ฒด์ ์ธ ๋กœ์ง์„ ๋จผ์ € ๊ตฌ์ƒํ•˜๊ณ , ์„ธ์„ธํ•œ ๋ถ€๋ถ„์„ ํ•˜๋‚˜์”ฉ ๊ตฌํ˜„ํ•˜๋Š” Top-down ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”.. ๋ฌธ์ œ ๋ถ„ํ• ์„ ์ž˜ ํ•˜๊ณ  ๋ถ„ํ• ๋œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž˜ ์„ธ์šฐ๋Š”๊ฒŒ ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
์ด๋ฒˆ ๋ฌธ์ œ๋„ Reverse๋ฅผ ์–ด๋–ป๊ฒŒ ์ž˜ ๊ตฌํ˜„ํ•˜๋Š๋ƒ๊ฐ€ ๋ฉ”์ธ ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์•„์š”! ์‹ค์ œ ์š”์†Œ๋“ค์„ ๋‹ค ๋ฐ”๊ฟ€ ํ•„์š” ์—†์ด ํฌ์ธํ„ฐ๋ฅผ ์ž˜ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋Š” ๊ทธ๋Œ€๋กœ ๋‘๊ณ  ์›ํ•˜๋Š” ๋ฐฉ์‹๋Œ€๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์€ ์ž์ฃผ ์‚ฌ์šฉ๋˜๊ณ , ๋‹ค์–‘ํ•˜๊ฒŒ ์‘์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์—ฐ์Šต ๋งŽ์ด ํ•ด๋ณด์‹œ๋Š”๊ฑธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants