-
Notifications
You must be signed in to change notification settings - Fork 2
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
25-alstjr7437 #190
25-alstjr7437 #190
Conversation
์ ๋ค์ python์ผ๋ก ๋์์จ...? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ด์ฌ ๋ฌธ์์ด ํ์ฑ ์ฒ๋ฆฌ ์ฌ์ด ๊ฑฐ ๋ถ๋ฝ๋ค์... ์์ ์ c++๋ก ํ์์ ๋ ๋ฌธ์์ด ์ฒ๋ฆฌํ๋ค๊ณ ์ฐธ ๊ท์ฐฎ์์๋๋ฐ...
#include <iostream>
#include <string>
#include <queue>
#include <sstream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int T; cin >> T;
while(T--) {
string p; cin >> p;
int n; cin >> n;
string arr; cin >> arr;
deque<int> dq;
string new_arr = string(arr.begin() + 1, arr.end() - 1);
istringstream ss(new_arr);
string buffer;
while(getline(ss, buffer, ','))
dq.emplace_back(stoi(buffer));
bool reverse = false;
bool error = false;
for(const auto& ch : p) {
if(ch == 'R') {
reverse = !reverse;
} else if(ch == 'D') {
if(dq.empty()) {
cout << "error\n";
error = true;
break;
} else {
reverse ? dq.pop_back() : dq.pop_front();
}
}
}
if(error)
continue;
cout << "[";
if(!dq.empty()) {
int end;
if(reverse) {
end = dq.front();
dq.pop_front();
while(!dq.empty()) {
cout << dq.back() << ",";
dq.pop_back();
}
} else {
end = dq.back();
dq.pop_back();
while(!dq.empty()) {
cout << dq.front() << ",";
dq.pop_front();
}
}
cout << end;
}
cout << "]\n";
}
return 0;
}
|
||
p = input() | ||
n = int(input()) | ||
x = deque(input().strip()[1:-1].split(',')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
n์ด 0์ธ ๊ฒฝ์ฐ์ ๋ํ ์์ธ ์ฒ๋ฆฌ๋ง ํด์ฃผ๋ฉด, ๊ตณ์ด strip()์ ์๋ถ์ฌ๋ ๋๊ธด ํฉ๋๋ค :)
if n == 0:
input() # ํด๋น ์
๋ ฅ์ ๋ฒ๋ฆผ
x = deque()
else:
x = deque(list(input()[1:-2].split(',')))
# ๋๋
try:
# n == 0์ธ ์
๋ ฅ์ด ์ฃผ์ด์ง ๊ฒฝ์ฐ, ๋น ๋ฌธ์์ด์ ๋ํด int๋ก mapping ํ๊ธฐ ๋๋ฌธ์ ์๋ฌ ๋ฐ์
x = deque(map(int, input()[1:-2].split(',')))
except:
x = deque() # ์๋ฌ ๋ฐ์ ์ empty deque์ ๋์
if temp_reverse : | ||
temp_reverse = False | ||
else : | ||
temp_reverse = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xor์ ์ด์ฉํด ๊น๋ํ๊ฒ ํ ์ค๋ก ์ค์ฌ๋ด ์๋ค
temp_reverse ^= True
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ฐ๋ ์ฑ์ด ์คํ๋ ค ์ค์ง ์์๊น์..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฐ๋ก python temp_reverse = not tmp_reverse
๋ฅผ ํ๋๊ฒ๋?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฐ๋ก temp_reverse = not tmp_reverse๋ฅผ ํ๋๊ฒ๋?!
๋์ํฉ๋๋ค...!
if temp_reverse : | ||
x.reverse() | ||
|
||
if error == 0 : | ||
print(f"[{','.join(str(i) for i in x)}]") | ||
else : | ||
print("error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋ฌ์ ๋ํ ์ถ๋ ฅ ์ฒ๋ฆฌ๋ฅผ ์๋ก ์ฌ๋ ค๋ฒ๋ฆฌ๋ ๊ฒ ๋ ๊น๋ํ ๊ฒ ๊ฐ๋ค์.
๋, ์ด์ฐจํผ ๋ฑ์ ๋ค์ด์๋ ์์๋ค์ด ๋ฌธ์์ด๊ธฐ ๋๋ฌธ์ ๋ค์ด๋ ํธ๋ก ๋ฌธ์์ด join ํ ์ ์์ต๋๋ค.
if error:
print('error')
continue
if temp_reverse:
x.reverse()
print(f"[{','.join(x)}]")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wkdghdwns199 ๋์ํฉ๋๋ค~~~~~~~~~~~ ๋ณด๊ฐํฉ๋๋ค~~~~~~~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wkdghdwns199 ๋์ํฉ๋๋ค~~~~~~~~~~~ ๋ณด๊ฐํฉ๋๋ค~~~~~~~~
์์ด ํ์คjang...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์ค์ ใ ใ ใ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from collections import deque
import sys
def input(): return sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
queries = deque(input())
n = int(input())
numbers = deque(input().strip("[").strip("]").replace(",", " ").split())
reverse = False
while queries:
now_query = queries.popleft()
if now_query == "R": reverse = not reverse
elif now_query == "D" and len(numbers) >= 1:
if reverse: numbers.pop()
else: numbers.popleft()
else:
print("error", end="")
break
else:
print("[", end="")
if reverse: print(",".join(reversed(numbers)), end="")
else: print(",".join(numbers), end="")
print("]", end="")
print()
๋ฌธ์ ๋ณด์๋ง์ ๋ฐ๋ก ํ์ด ๋ ์ฌ๋ ธ๊ณ
๋ฐ๋ก ์ํ๋๋ฐ
๋ฐ๋ก ๊ณจ์ธ ํคํคํคํคํผ
์ค์~~~~~~~~~~~~~~~~~~~~~~~~
ํ๋งํ์๋๋ค.
|
||
p = input() | ||
n = int(input()) | ||
x = deque(input().strip()[1:-1].split(',')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ ์ด๋ฐ ๋ฐฉ๋ฒ์ด................................
if temp_reverse : | ||
x.reverse() | ||
|
||
if error == 0 : | ||
print(f"[{','.join(str(i) for i in x)}]") | ||
else : | ||
print("error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wkdghdwns199 ๋์ํฉ๋๋ค~~~~~~~~~~~ ๋ณด๊ฐํฉ๋๋ค~~~~~~~~
์ ์ธ์ด ์ฐจ์ด ์ฝ๋ ๊ธธ์ด ์ฐจ์ด ๋๋ฐ ใ ใ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ isReverse ๋ณ์ ๋ง๋ค์ด ์์์ ๋นผ๊ณ ๋ค์์ ๋นผ๊ณ ํ์ต๋๋ค! ๋ฐฉ๋ฒ์ ๋๊ฐ์์. ๊ทผ๋ฐ ํ๋ฆฐํธ ์๊ฐ์ด๊ณผ ๋๋ฌธ์ BufferedWrite ๋ฅผ ์ฌ์ฉํด ๋ฌธ์๋ฅผ ๋ค ์ถ๊ฐํ๊ณ flush ํ์ต๋๋ค..!
0 ๋ถ๊ธฐ ๋๋ฌธ์ ์คํจํ๊ฑฐ ํน๋ฐ๋ค์.
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
private lateinit var br: BufferedReader
private lateinit var bw: BufferedWriter
fun main() {
br = BufferedReader(InputStreamReader(System.`in`))
bw = BufferedWriter(OutputStreamWriter(System.out))
val t = br.readLine().toInt()
repeat(t) {
ac()
}
}
fun ac() {
val instruction = br.readLine().chunked(1)
val n = br.readLine().toInt()
val input = br.readLine()
val queue = ArrayDeque<Int>()
if (n != 0) {
input.substring(1, input.length - 1).split(",").forEach { queue.add(it.toInt()) }
}
var isReversed = false
for (i in instruction.indices) {
when {
!isReversed && instruction[i] == "R" -> isReversed = true
isReversed && instruction[i] == "R" -> isReversed = false
instruction[i] == "D" -> {
if (queue.isEmpty()) {
println("error")
return
}
if (isReversed) {
queue.removeLast()
continue
}
queue.removeFirst()
}
}
}
printQueue(queue, isReversed)
}
private fun printQueue(queue: ArrayDeque<Int>, isReversed: Boolean) {
bw.write("[")
while (queue.isNotEmpty()) {
bw.write("${if (isReversed) queue.removeLast() else queue.removeFirst()}")
if (queue.isEmpty()) {
break
}
bw.write(",")
}
bw.write("]")
bw.newLine()
bw.flush()
}
if temp_reverse : | ||
temp_reverse = False | ||
else : | ||
temp_reverse = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ฐ๋ ์ฑ์ด ์คํ๋ ค ์ค์ง ์์๊น์..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฏผ์๋ PR๊ณผ ๋๊ฐ์ ๊ณผ์ ์ ๊ฒช์๋ค์
- R์ด ๋ค์ด์ฌ๋ reverse๋ฅผ ํ๋๋ก ํ๋๋ ๋๊ฐ์ด ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ์ต๋๋ค
์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊น ํ๋ค๊ฐ ๋ฐ๋ก reverse์ ๋ํ flag ์ ํธ๋ฅผ ๋๊ณ , T/F๋ก ๋ฐ๊พธ๋๋ก ํด์ ๋ง์ง๋ง ์ถ๋ ฅํ๊ธฐ ์ ์ ํ๋ฒ๋ง reverse๊ฐ ๋๋๋ก ํ์์ต๋๋ค.
๊ทธ๋์ deque์์ ๋นผ๋ ๊ฒ๋ reverse_check์ด True์ด๋ฉด pop()์ ํด์ฃผ์์ด์
์ค๊ฐ์ error์ผ๋ break์ ๋นผ๋จน์ด์ ํ๋ ธ์ต๋๋ค๊ฐ ๋ช๋ฒ ๋์๋ค์ ํํ
์๊ณ ํ์
จ์ต๋๋ค ~~~
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
p = input()
n = int(input())
arr = deque(input().strip("[").strip("]").replace(",", " ").split())
reverse_check = False
error_check = 0
for i in range(len(p)):
if p[i] == "R":
#arr.reverse()
reverse_check = not reverse_check
else:
if len(arr) != 0:
if reverse_check: # reverse๋ฉด ์ ์์๊ฐ ์๋๋ผ ๋ค ์์๋ฅผ ์ ๊ฑฐ
arr.pop()
else:
arr.popleft()
else:
error_check = 1
print("error")
break
if not error_check:
if reverse_check:
arr.reverse()
print("[" + ",".join(arr) + "]")
if temp_reverse : | ||
temp_reverse = False | ||
else : | ||
temp_reverse = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฐ๋ก python temp_reverse = not tmp_reverse
๋ฅผ ํ๋๊ฒ๋?!
๐ ๋ฌธ์ ๋งํฌ
AC
โ๏ธ ์์๋ ์๊ฐ
40๋ถ
โจ ์๋ ์ฝ๋
๋ฌธ์ ๋ ๊ฐ๋จํด์ ์์ด๋์ด๋ ๋ฐ๋ก ๋ ์ฌ๋์ต๋๋ค!
์ฒ์์๋
ํ์ง๋ง ๊ณจ๋5 ๋ฌธ์ ๊ฐ ์ด๋ ๊ฒ ์ฝ๊ฒ ํ๋ฆฐ๋ค๊ณ ์๊ฐํ๋ฉด์ ์ ์ถ์ ํ๋๋ฐ
์ญ์ ์๋์ ๊ฐ์ด ์๊ฐ์ด๊ณผ์ ๋ช์ ๋น ์ก์ต๋๋ค.
์ ์๊ฐํด๋ณด๋ฉด reverse์ reversedํจ์์ ์๊ฐ๋ณต์ก๋๋ O(N)์ธ๋ฐ ๋งค๋ฒ ๋ค์ง์ด์ฃผ๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋ฉ๋๋ค!
๐ฅ ์๊ฐ ์ค์ด๋ ์ฝ๋
์ด๋ ๊ฒ ํด์ ์์ฑ์ ํ๋๋ฐ
๊ฐ์๊ธฐ 16%์์ ํ๋ ธ๋ค๊ณ ๊ณ์ ๋์ค๊ธธ๋ ํ์ธํด๋ณด๋
์์ ์๋ฌ๊ฐ ๋ฌ์ต๋๋ค.
๋ฌธ์ ์์ ๊ณต๋ฐฑ์ด ์๊ธธ๋ ์ ๋ ๊ฒ ํด๋ ๋๋์ค ์์๋๋ฐ
์๋์ ๊ฐ์ด strip์ผ๋ก ๊ณต๋ฐฑ์ ์์ ๋ ์ฝ๋๋ก ๋ฐ๊พธ๋๊น ๋๋๊ตฐ์..
์ด๊ฑฐ ์ฐพ๋๋ผ๊ณ ํ 10๋ถ ๋ฒ๋ฆฐ๋ฏ...
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
๋ฑ์ด๋ ๊ทธ๋ฐ ์ฌ๋ฌ๊ฐ์ง ์๋ฃ๊ตฌ์กฐ ๋ฌธ์ ์์ ์๊ฐ๋ณต์ก๋๋ฅผ ์์๊ฐํ๊ณ ํ์!!!
ํน์ ์๊ฐ๋ณต์ก๋ ๊ณ์ฐ์ ์ด๋ป๊ฒ ํ๋ฉด๋๋์..?
O(N)(์์๊ฐฏ์) * O(P)(๋ช ๋ น์ด R์ ๊ฐฏ์)๋ก ํด์ ์๊ฐ์ด๊ณผ๊ฐ ๋๋๊ฑฐ ๊ฒ ์ฃ ?