Skip to content

Conversation

@caucsejunseo
Copy link
Collaborator

πŸ”— 문제 링크

https://www.acmicpc.net/problem/14713

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

3h

✨ μˆ˜λ„ μ½”λ“œ

λ¨Όμ € ꡬ쑰체λ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€!

ꡬ쑰체 ν•˜λ‚˜κ°€ μ•΅λ¬΄μƒˆμ— λŒ€μ‘ν•˜κ²Œ 되고, ꡬ쑰체 μ•ˆμ˜ 2차원 λ°°μ—΄ words의 행은 λ¬Έμžμ—΄μ˜ 단어 개수, 열은 λ¬Έμžμ—΄μ˜ 문자λ₯Ό μ €μž₯ν•©λ‹ˆλ‹€. 사싀상 3차원 배열을 μ‚¬μš©ν•œ 것과 κ°™μŠ΅λ‹ˆλ‹€.
image

λ¬Έμžμ—΄μ„ 띄어쓰기 κΈ°μ€€μœΌλ‘œ λ‚˜λˆ„κ³ , 맨 μ•žμ˜ 단어λ₯Ό enqueueν•˜λ©° μ €μž₯ν•©λ‹ˆλ‹€.
μ•΅λ¬΄μƒˆμ˜ λ¬Έμžμ—΄μ„ μ „λΆ€ μ €μž₯ν•˜μ˜€μœΌλ©΄ 이제 μ •λ‹΅ λ¬Έμžμ—΄κ³Ό λΉ„κ΅ν•˜λ©΄ λ©λ‹ˆλ‹€.
(μ €λŠ” λ¬Έμžμ—΄μ„ μ €μž₯ν•˜λŠ” 것이 κ°€μž₯ μ–΄λ €μ› μŠ΅λ‹ˆλ‹€..)

μ •λ‹΅ λ¬Έμžμ—΄μ˜ 맨 μ•ž 단어와 각각 μ•΅λ¬΄μƒˆκ°€ κ°€μ§€κ³  μžˆλŠ” 맨 μ•žμ˜ 단어가 μΌμΉ˜ν•˜λŠ” μ§€λ₯Ό λΉ„κ΅ν•©λ‹ˆλ‹€.

for문을 돌며 μ•΅λ¬΄μƒˆμ˜ 맨 μ•ž 단어와 λΉ„κ΅ν•˜κ³  μΌμΉ˜ν•˜λŠ” 게 μžˆλ‹€λ©΄ κ·Έ 단어λ₯Ό λΉΌμ£Όλ©΄ λ©λ‹ˆλ‹€.
( λ¨Όμ € λ“€μ–΄κ°„ λ¬Έμžκ°€ κ°€μž₯ λ¨Όμ € λ‚˜μ˜¨λ‹€λŠ” μ μ—μ„œ 큐! )

μ•΅λ¬΄μƒˆκ°€ κ°€μ§„ λ¬Έμžμ—΄μ„ λ‹€ μ‚¬μš©ν•˜μ§€ μ•Šκ³  μ •λ‹΅ λ¬Έμžμ—΄μ„ μ°Ύμ•˜μ„ μˆ˜λ„ μžˆμœΌλ―€λ‘œ κ·Έ 뢀뢄을 μ˜ˆμ™Έμ²˜λ¦¬ ν•΄μ€€λ‹€λ©΄ 끝!

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

띄어쓰기λ₯Ό ν¬ν•¨ν•œ λ¬Έμžμ—΄μ„ λ°›κΈ° μœ„ν•œ fgets()λ₯Ό μ•Œκ²Œ λ˜μ—ˆλ‹€.

@froglike6
Copy link
Collaborator

μ €λŠ” 각 μ•΅λ¬΄μƒˆμ˜ 말을 deque에 λ‹΄κ³ , 각 μ•΅λ¬΄μƒˆμ˜ 0번 인덱슀 단어와 μ •λ‹΅ λ¬Έμžμ—΄ 단어λ₯Ό λΉ„κ΅ν•˜λ©° μ§„ν–‰ν–ˆμŠ΅λ‹ˆλ‹€.
μ•Œκ³ λ¦¬μ¦˜μ€ λ˜‘κ°™μ§€λ§Œ μ €μž₯ν•˜λŠ” 방법이 λ‹€λ₯΄λ„€μš”

Python3

from collections import deque
import sys
input = sys.stdin.readline
N = int(input())
parrot = []
for _ in range(N):
    tmp = deque(input().split())
    parrot.append(tmp)

L = input().split()

possible = True

for word in L:
    found = False
    for p in parrot:
        if p and p[0] == word:
            p.popleft()
            found = True
            break
    if not found:
        possible = False
        break

if possible:
    for p in parrot:
        if p:
            possible = False
            break

if possible:
    print("Possible")
else:
    print("Impossible")

μž…λ ₯받을 λ•Œ μ—­μˆœμœΌλ‘œ μ •λ ¬ν•΄μ„œ μŠ€νƒμ— 넣어도 λ¬Έμ œκ°€ 풀릴 것 κ°™λ„€μš”

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

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

각각의 μ•΅λ¬΄μƒˆκ°€ ν•˜λŠ” 말을 각각 λ‹€λ₯Έ 큐에 μ €μž₯ν•˜κ³  μ‹Άμ—ˆλŠ”λ°, νŒŒμ΄μ¬μ€ 그게 κ°€λŠ₯ν•˜λ”κ΅°μš”! 각각 N개의 독립적인 큐λ₯Ό λ§Œλ“€μ–΄μ„œ queue[i] λ₯Ό 톡해 μ ‘κ·Όν•˜μ—¬ λΉ„κ΅ν•΄λ΄€μŠ΅λ‹ˆλ‹€.

from collections import deque
import sys
def junjun():
    N = int(input())
    q = [deque() for _ in range(N)]
    Lq = deque()


    for i in range(N):
        bird = []
        bird = sys.stdin.readline().strip().split()
        q[i] = deque(bird)


    Lq = deque(sys.stdin.readline().strip().split())
    gojunglength = len(Lq)
    
    while Lq:
        found = False
        for j in range(N):
            if q[j] and q[j][0] == Lq[0]:
                q[j].popleft()
                Lq.popleft()
                found = True
                break 
        if not found:
            print("Impossible")
            return
    for i in range(N):
        if q[i]:  
            print("Impossible")
            return

    print("Possible")

junjun()

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

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

였호 접근보닀 κ΅¬ν˜„μ΄ 더 μ€‘μš”ν•œ 문제 κ°™λ„€μš”..!
μ €λŠ” κ΅¬ν˜„μ—μ„œ λ§‰ν˜€μ„œ sstreamμ΄λΌλŠ” 라이브러리λ₯Ό μ΄μš©ν•˜μ—¬ ν•΄κ²°ν•˜μ˜€μŠ΅λ‹ˆλ‹€...
sstream λΌμ΄λΈŒλŸ¬λ¦¬λŠ” λ¬Έμžμ—΄μ—μ„œ 값을 μΆ”μΆœν•˜κ±°λ‚˜ μ‚½μž…ν•  λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
이번 λ¬Έμ œμ—μ„œλŠ” λ¬Έμžμ—΄μ„ νŒŒμ‹±ν•  λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μ•„λ¬΄λž˜λ„ 언어에 λŒ€ν•œ 이해와 κ΅¬ν˜„ λŠ₯λ ₯을 ν‚€μ›Œμ•Όκ² μŠ΅λ‹ˆλ‹€.😒

C++ μ½”λ“œ
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
using namespace std;

int main() {
    ios::sync_with_stdio(false); cin.tie(NULL);
    
    int N, wordCount = 0;
    cin >> N;
    //μž…λ ₯ ν›„ 남은 λ²„νΌμ˜ κ°œν–‰λ¬Έμžλ₯Ό λ¬΄μ‹œν•˜κΈ°
    cin.ignore();
    queue<string> S[N];
    string str, word;
    
    for(int i = 0; i < N; i++) {
        getline(cin, str);
        //곡백 κΈ°μ€€μœΌλ‘œ λ¬Έμžμ—΄ νŒŒμ‹±
        stringstream stream(str);
        //μž…λ ₯ μŠ€νŠΈλ¦Όμ—μ„œ 값을 μ½μ–΄μ˜€λŠ” μ—°μ‚°μž '>>'
        while(stream >> word) {
            S[i].push(word);
            wordCount++;
        }
    }
    
    getline(cin, str);
    stringstream stream(str);
    queue<string> L;
    while(stream >> word)
        L.push(word);
    
    if(L.size() != wordCount) {
        cout << "Impossible" << '\n';
        return 0;
    }
    
    int size = L.size();
    for(int i = 0; i < size; i++) {
        bool valid = false;
        for(int j = 0; j < N; j++) {
            if(!S[j].empty()) {
                if(L.front() == S[j].front()) {
                    L.pop();
                    S[j].pop();
                    valid = true;
                    break;
                }
            }
        }
        if(!valid) {
            cout << "Impossible" << '\n';
            return 0;
        }
    }

    cout << "Possible" << '\n';
    return 0;
}

PR에 μˆ˜λ„ μ½”λ“œλ₯Ό 직접 μž‘μ„±ν•΄λ³΄λŠ” κ±Έ μΆ”μ²œλ“œλ¦½λ‹ˆλ‹€!
λ‚΄κ°€ μž‘μ„±ν•œ μ½”λ“œλ₯Ό λͺ¨λ₯΄λŠ” μ‚¬λžŒμ΄ 봐도 이해할 수 μžˆλ„λ‘ ν•˜λŠ” 것이 μˆ˜λ„ μ½”λ“œ μž‘μ„±μ˜ μ·¨μ§€μž…λ‹ˆλ‹€.

λ‚œμ΄λ„κ°€ μ˜¬λΌκ°€λ©΄, 언어에 λŒ€ν•œ 이해가 λΆ€μ‘±ν•œ 경우 μ½”λ“œλ₯Ό λͺ» μ½λŠ” κ²½μš°λ„ 생길 수 μžˆκ±°λ“ μš”.
λ˜ν•œ μˆ˜λ„ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄μ„œ 본인의 μ½”λ“œλ₯Ό 더 잘 이해할 수 있고 μ„€λͺ…ν•˜κΈ°λ„ μ‰¬μ›Œμ§‘λ‹ˆλ‹€!
μ €λŠ” μˆ˜λ„ μ½”λ“œλ₯Ό λ¨Όμ € μ“°κ³  PR μ„€λͺ…을 μ“°λŠ” νŽΈμ΄μ—μš” :)

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.

6 participants