본문 바로가기
둥지/알고리즘

[백준 15723] n단 논법

by 까닭 2023. 7. 14.

문제링크

#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    bool arr[26][26] = {};

    int n = 0;
    cin >> n;

    //전제
    for (size_t i = 0; i < n; ++i)
    {
        string str = {};
        cin >> str;
        int a = str[0] - 'a';
        cin >> str; //is
        cin >> str;
        int b = str[0] - 'a';

        arr[a][b] = true;
    }
    
    //플로이드 와샬
    for (size_t via = 0; via < 26; ++via)
    {
        for (size_t from = 0; from < 26; ++from)
        {
            for (size_t to = 0; to < 26; ++to)
            {
                if (arr[from][via] && arr[via][to])
                {
                    arr[from][to] = true;
                }
            }
        }
    }

    int m = 0;
    cin >> m;

    string result = {};

    for (size_t i = 0; i < m; i++)
    {
        string str = {};
        cin >> str;
        int a = str[0] - 'a';
        cin >> str; //is
        cin >> str;
        int b = str[0] - 'a';

        if (true == arr[a][b])
        {
            result += "T";
        }

        else
        {
            result += "F";
        }
    }

    for (size_t i = 0; i < result.size(); i++)
    {
        cout << result[i] << '\n';
    }

    return 0;
}

'둥지 > 알고리즘' 카테고리의 다른 글

[백준 15482]한글 LCS  (0) 2023.06.19
[백준 11404] 플로이드  (1) 2023.06.09
[백준 11726] 2×n 타일링  (0) 2023.05.26
[프로그래머스] 폰켓몬  (0) 2023.05.08
[프로그래머스] 프로세스  (0) 2023.05.04