ACM-ICPC 2018 沈阳赛区网络预赛 I 题 Lattice's basics in digital electronics

原题链接:https://nanti.jisuanke.com/t/31450

附上队友代码:(感谢队友带飞)

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define DOR(i,a,b) for(int i=(a);i>=(b);--i) 
const int maxN=2e5+5,inf=0x3f3f3f3f;
int N, M, K, T;
int g[maxN];
unordered_map<string,int> mp;
char S1[maxN * 4], S2[maxN * 4], S3[maxN * 4];

struct ND{
    ND *lch, *rch;
    char x;
};

char cg[20][5] = {"0000","0001","0010", "0011", "0100",
    "0101", "0110", "0111", "1000", "1001", "1010",
    "1011", "1100", "1101", "1110", "1111"};

bool judge(char *s, int st) {
    int i = 0, one = 0;
    while (i < 9 && s[st + i] != 0) {
        if (s[st + i] == '1')
            ++one;
        ++i;
    }
    if (i < 9) return 0;
    if (one & 1) return 1;
    else return 0;
}

int main () {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    scanf("%d", &T);
    while (T--) {
        S1[0] = S2[0] = S3[0] = 0;
        mp.clear();
        scanf("%d%d", &M, &N);
        int a;
        char s[15];
        FOR(i, 1, N) {
            scanf("%d %s", &a, s);
            mp[s] = a;
        }
        scanf("%s", S1);
        for (int i = 0; S1[i] != 0; ++i) {
            int cnt;
            if (isdigit(S1[i])) cnt = S1[i] - '0';
            else {
                S1[i] = toupper(S1[i]);
                cnt = S1[i] - 'A' + 10;
            }
            FOR(j, 0, 3) {
                S2[i * 4 + j] = cg[cnt][j];
            }
        }
        // puts(S1);
        // puts(S2);
        int L = strlen(S2), cnt = 0;
        for (int i = 0; i < L; i += 9) {
            bool ok = judge(S2, i);
            if (ok) {
                for (int j = i; j < i + 8; ++j)
                    S3[cnt++] = S2[j];
            }
        }
        S3[cnt] = 0;
        // cout << "cnt" << cnt << endl;

        /*
        for (int i = 0; i < cnt; ++i) {
            if (i && i % 4 == 0) printf(" ");
            printf("%c",S3[i]);
        }
        */
        //puts(S3);
        string str = S3;
        int L3 = str.length();
        int idx = 0;
        int all = 0;
        while (idx < L3) {
            for (int len = 1; len <= 10; ++len) {
                if (L3 - idx + 1 < len) {
                    idx = L3;
                    break;
                }
                string subs = str.substr(idx, len);
                if (mp.count(subs)) {
                    printf("%c", mp[subs]);
                    ++all;
                    
                    if (all == M) {
                        idx = L3;
                        break;
                    }
                    
                    idx += subs.length();
                    break;
                }
            }
        }
        puts("");
        
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/linruier2017/article/details/82533172