티스토리 뷰

반응형


#. Problem


* The copyright in this matter is in BOJ



#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
= input()
tmp = ''
ans = ''
isTag = False
 
for i in S:
    if i == ' ':
        ans += tmp[::-1+ i
        tmp = ''
    elif i == '<':
        isTag = True
        ans += tmp[::-1+ i
        tmp = ''
    elif i == '>':
        isTag = False
        ans += i
    elif isTag:
         ans += i
    else:
        tmp += i
 
ans += tmp[::-1]
 
print(ans)
cs


조건이 많으니까 은근 헷갈린다..


#. Other Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
= input()
 
tmp, ans, ck = '''', False
 
for i in S:
    if i == ' ':
        if not ck:
            ans += tmp[::-1+ i
            tmp = ''
        else: ans += i
    elif i == '<':
        ck = True
        ans += tmp[::-1+ i
        tmp = ''
    elif i == '>':
        ck = False
        ans += i
    else:
        if ck: ans += i
        else: tmp += i
 
ans += tmp[::-1]
print(ans)
cs


line 6, 18 에서 나는 tag 체크를 안 했는데.. 어떻게 통과했넹ㅋㅋㅋ

반례나오면 큰일났겠구먼..

래도 이렇게 안전하게 코드를 짜는게 중요한 것 같다.


line 6~10) 공백이 나올 경우

line 7) tag 내부가 아닐 경우 역tmp 를 ans에 추가해주고 tmp 초기화

line 10) tag 내부일 경우 바로 ans에 추가

line 11~14) tag가 시작될 경우

line 12) tag 상태 체크

line 13) 이전까지 역tmp를 ans에 추가해주고 tmp 초기화

line 15~17) tag가 끝날 경우

line 16) tag 상태 해제

line 17) ans 변수에 추가

line 18~20) 이외의 경우

line 19) tag 내부일 경우 ans 변수에 바로 추가

line 20) tag 내부가 아닐 경우 tmp 변수에 추가

line 22) 반복문이 끝나고 출력되지 않은 역tmp를 ans에 추가

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday