【ACM-ICPC 2018 沈阳赛区网络预赛】I.Lattice's basics in digital electronics ---- 字典树

题目传送门

做法: 用字典树存好译码词,然后模拟即可

AC代码:

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

#define IO          ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x)       push_back(x)
#define sz(x)       (int)(x).size()
#define sc(x)       scanf("%d",&x)
#define abs(x)      ((x)<0 ? -(x) : x)
#define all(x)      x.begin(),x.end()
#define mk(x,y)     make_pair(x,y)
#define fin         freopen("in.txt","r",stdin)
#define fout        freopen("out.txt","w",stdout)

typedef long long ll;
typedef pair<int,int> P;
const int mod = 1e9+7;
const int maxm = 1e8+5;
const int maxn = 1e6+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;

int trie[maxn][2],cnt,tot;
int val[maxn],num[maxn],ans[maxn];
char s[maxn];
inline void add(char *s,int k)
{
    int rt = 0;
    for(int i=0;s[i];i++)
    {
        int x = s[i]-'0';
        if(trie[rt][x] == 0) trie[rt][x] = ++cnt;
        rt = trie[rt][x];
    }
    val[rt] = k;
}
int main()
{
//    fin;
    IO;
    int t;
    cin>>t;
    while(t--)
    {
        memset(val,0,sizeof(val));
        memset(trie,0,sizeof(trie));
        tot = 0;
        cnt = 0;
        int n,m,x;
        cin>>n>>m;
        char str[15];
        for(int i=0;i<m;i++)
        {
            cin>>x>>str;
            add(str,x);
        }
        cin>>s;
        for(int i=0;s[i];i++)
        {
            int now;
            if(s[i]>='0' &&s[i]<='9') now = s[i]-'0';
            else if(s[i]>='a' && s[i]<='z') now = s[i]-'a'+10;
            else if(s[i]>='A' && s[i]<='Z') now = s[i]-'A'+10;
            num[++tot] = (now>>3)&1;
            num[++tot] = (now>>2)&1;
            num[++tot] = (now>>1)&1;
            num[++tot] = now&1;
        }
        int cnt2 = 0;
        for(int i=1;i<=tot;i+=9)
        {
            int k = 0;
            for(int j=i;j<=i+7;j++) if(num[j]) k++;
            if((k&1) && num[i+8] == 0 || (k&1) == 0 && num[i+8] == 1)
            {
                for(int j=i;j<=i+7;j++) ans[++cnt2] = num[j];
            }
        }
        int tot2 = 0,rt = 0;
        for(int i=1;i<=cnt2;i++)
        {
            rt = trie[rt][ans[i]];
            if(val[rt]){
                cout<<(char)val[rt];
                tot2++;
                rt = 0;
                if(tot2 == n) break;
            }
        }
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值