1. 程式人生 > >【ACM-ICPC 2018 瀋陽賽區網路預賽】I.Lattice's basics in digital electronics ---- 字典樹

【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; }