202009-3 点亮数字人生

举报
辰chen 发表于 2022/06/15 00:25:03 2022/06/15
【摘要】 文章目录 C++总结 本题链接:202009-3 点亮数字人生 本博客给出本题截图: C++ #include <iostream> #include <cst...

文章目录


本题链接202009-3 点亮数字人生

本博客给出本题截图
在这里插入图片描述

C++

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 3010, M = N * 5;

int m, n;
int w[N], f[N];
int h[N], e[M], ne[M], idx;
int q[N], d[N];
vector<int> in[M], out[M];

int get(char* str)
{
    string names[] = {
        "AND", "OR", "NOT", "XOR", "NAND", "NOR"
    };
    for (int i = 0; i < 6; i ++ )
        if (names[i] == str)
            return i;
    return -1;
}

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
    d[b] ++ ;
}

bool topsort()
{
    int hh = 0, tt = -1;
    for (int i = 1; i <= m + n; i ++ )
        if (!d[i])
            q[ ++ tt] = i;
    while (hh <= tt)
    {
        int t = q[hh ++ ];
        for (int i = h[t]; ~i; i = ne[i])
        {
            int j = e[i];
            if ( -- d[j] == 0)
                q[ ++ tt] = j;
        }
    }
    return tt == m + n - 1;
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T -- )
    {
        scanf("%d%d", &m, &n);
        memset(h, -1, sizeof h);
        idx = 0;
        memset(d, 0, sizeof d);
        char str[100];
        for (int i = 1; i <= n; i ++ )
        {
            int cnt;
            scanf("%s%d", str, &cnt);
            f[m + i] = get(str);
            while (cnt -- )
            {
                scanf("%s", str);
                int t = atoi(str + 1);
                if (str[0] == 'I') add(t, m + i);
                else add(m + t, m + i);
            }
        }
        int Q;
        scanf("%d", &Q);
        for (int i = 0; i < Q; i ++ )
        {
            in[i].clear();
            for (int j = 0; j < m; j ++ )
            {
                int x;
                scanf("%d", &x);
                in[i].push_back(x);
            }
        }
        for (int i = 0; i < Q; i ++ )
        {
            out[i].clear();
            int cnt;
            scanf("%d", &cnt);
            while (cnt -- )
            {
                int x;
                scanf("%d", &x);
                out[i].push_back(x);
            }
        }
        if (!topsort()) puts("LOOP");
        else
        {
            for (int i = 0; i < Q; i ++ )
            {
                for (int j = 0; j < m; j ++ ) w[j + 1] = in[i][j];
                for (int j = m + 1; j <= m + n; j ++ )
                    if (f[j] == 0 || f[j] == 5) w[j] = 1;
                    else w[j] = 0;
                for (int j = 0; j < m + n; j ++ )
                {
                    int t = q[j], v = w[t];
                    for (int k = h[t]; ~k; k = ne[k])
                    {
                        int u = e[k];
                        if (f[u] == 0) w[u] &= v;
                        else if (f[u] == 1) w[u] |= v;
                        else if (f[u] == 2) w[u] = !v;
                        else if (f[u] == 3) w[u] ^= v;
                        else if (f[u] == 4) w[u] |= !v;
                        else w[u] &= !v;
                    }
                }
                for (auto x: out[i])
                    printf("%d ", w[m + x]);
                puts("");
            }
        }
    }
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130

总结

本题在大模拟的基础上加入了拓扑排序
做了这么多道大模拟的题目,码代码能力确实提升了,就是调试起来会很上头😡
本题需要注意,最后在计算电路结果的时候也是需要考虑顺序的

文章来源: chen-ac.blog.csdn.net,作者:辰chen,版权归原作者所有,如需转载,请联系作者。

原文链接:chen-ac.blog.csdn.net/article/details/121868948

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。