[白俊]17413-単語反転2(Python)
17732 ワード
質問する
https://www.acmicpc.net/problem/17413
コード#コード#
import sys
input = list(sys.stdin.readline().rstrip())
result = []
temp = []
i = 0
while i < len(input):
if input[i] == '<':
if len(temp) != 0:
count = len(temp)
for j in range(count):
result.append(temp.pop())
result.append(input[i])
i += 1
while input[i] != '>':
result.append(input[i])
i += 1
result.append(input[i])
i += 1
elif input[i] == ' ':
if len(temp) != 0:
count = len(temp)
for j in range(count):
result.append(temp.pop())
result.append(input[i])
i += 1
else:
temp.append(input[i])
i += 1
if len(temp) != 0:
count = len(temp)
for j in range(count):
result.append(temp.pop())
print(''.join(result))
結果
整理する
コンセプト
入力した入力を一覧(?)n(enter)に含まれて格納されるため、入力を受信する際にrstrip()が使用される.
rstrip(「str」):strを文字列の右側から削除
1)strが入力されていません(=パラメータが入力されていません):文字列の右側にスペースがある場合は、スペースを削除します.
#1 인자가 없을때
print('Hello '.rstrip()) #Hello
2)strがハングル文字の場合:文字列の右側からその文字と同じすべての文字を削除する#2 인자가 한글자일때
print('apple'.rstrip('e')) #appl
3)strが複数文字の場合:文字列右側strのすべての組合せを削除する#3 인자가 여러글자일때
print('apple'.rstrip('lep')) #a
コード(コメントo)
import sys
input = list(sys.stdin.readline().rstrip())
# 문자열을 한글자씩 끊어서 리스트로 저장하기
# rstrip(): \n를 제외하고 저장하기 위해 사용
result = []
temp = []
i = 0
while i < len(input):
# '<' 를 만났을때
if input[i] == '<':
if len(temp) != 0: # temp의 길이가 0이 아닐때 = 뒤집어야 되는 단어가 있을때
count = len(temp)
for j in range(count): # temp의 길이만큼 반복
result.append(temp.pop()) # temp 리스트에서 pop해서 result 리스트에 append해줌
result.append(input[i]) # '<'를 append해줌
i += 1
# '>'를 만날때까지 반복
while input[i] != '>':
result.append(input[i]) # 괄호안에 있는 문자들은 뒤집지 않기 때문에 바로 result 리스트에 append해줌
i += 1
result.append(input[i]) # '>'를 append해줌
i += 1
# 띄어쓰기를 만났을때
elif input[i] == ' ':
if len(temp) != 0: # 위 코드와 동일
count = len(temp)
for j in range(count):
result.append(temp.pop())
result.append(input[i]) # 띄어쓰기를 append해줌
i += 1
# 그냥 평범한 글자일때 (=숫자, 문자일때)
else:
temp.append(input[i]) # 뒤집어야되는 글자들이기 때문에 result가 아닌 temp에 넣어줌
i += 1
# 입력받은 문자열로 while문을 돌고 나왔을때
# temp 길이가 0이 아니라는 것은 뒤집어야 되는 단어가 있다는 뜻
if len(temp) != 0: # 위 코드와 동일
count = len(temp)
for j in range(count):
result.append(temp.pop())
print(''.join(result))
に感銘を与える
最初はfor文で実現しようと思っていましたが、変数とif文が多くなって複雑になったようなのでwhile文に変えました.でもwhileゲートも複雑...for文で表現しても、複雑さはあまりありません😂 そのハサミを実現したのですが、コードが少し効率的ではないようで、、、ㅠㅠ後でこの問題を忘れた時にもう一度解こうと思ったのですが…!
Reference
この問題について([白俊]17413-単語反転2(Python)), 我々は、より多くの情報をここで見つけました https://velog.io/@alsdud5766/백준-17413-단어-뒤집기2-Pythonテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol