2018沈阳网络赛I

水题,进制转换之后用字典树模拟。

#include <bits/stdc++.h>
#define cls(x) memset(x,0,sizeof(x))
#define REP(i,x,y) for(i=x;i<=y;i++)
using namespace std;
const int M = 1e5+7;
const int MM = 1e4+7;
int t,len,n;
int i,j,tmp,tmp1;
int tree[MM][2];
int flg[MM];
int tot,cnt;
int ans[MM],ans1[M];
char s[M*8],ss[M*8];
void trans(){
        int ll=strlen(s);
        REP(i,0,ll-1){
            if(s[i]=='A'||s[i]=='a'){
                ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='0';
            }
            if(s[i]=='B'||s[i]=='b'){
                ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='1';
            }
            if(s[i]=='C'||s[i]=='c'){
                ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='0';
            }
            if(s[i]=='D'||s[i]=='d'){
                ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='1';
            }
            if(s[i]=='E'||s[i]=='e'){
                ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='0';
            }
            if(s[i]=='F'||s[i]=='f'){
                ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='1';
            }
            if(s[i]=='0'){
                ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='0';
            }
            if(s[i]=='1'){
                ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='1';
            }
            if(s[i]=='2'){
                ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='0';
            }
            if(s[i]=='3'){
                ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='1';
            }
            if(s[i]=='4'){
                ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='0';
            }
            if(s[i]=='5'){
                ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='1';
            }
            if(s[i]=='6'){
                ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='0';
            }
            if(s[i]=='7'){
                ss[tmp++]='0';ss[tmp++]='1';ss[tmp++]='1';ss[tmp++]='1';
            }
            if(s[i]=='8'){
                ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='0';
            }
            if(s[i]=='9'){
                ss[tmp++]='1';ss[tmp++]='0';ss[tmp++]='0';ss[tmp++]='1';
            }
        }
}
void ins(char *str,int x)
{
   int len=strlen(str);
   int root=0;
   for(int i=0;i<len;i++)
   {
       int id=str[i]-'0';
       if(!tree[root][id]) tree[root][id]=++tot;
       root=tree[root][id];
   }
   flg[root]=1;
   ans[root]=x;
}
void Query(char *str){
    int root=0;
    for(int i=0;i<tmp1;i++)
    {
        int id=str[i]-'0';
        if(flg[tree[root][id]]){
            ans1[++cnt]=ans[tree[root][id]];
            if(cnt==len) return ;
            root=0;
            continue;
        }
        root=tree[root][id];
    }
    return ;
}
int main(){
    scanf("%d",&t);
    while(t--){
        REP(i,0,tot){
           flg[i]=false;
           REP(j,0,1)
               tree[i][j]=0;
        }
        tmp=0;cnt=0;tmp1=0;
        tot=0;
        cls(s);cls(ss);cls(flg);
        scanf("%d%d",&len,&n);
        REP(i,1,n){
            int as;char s1[15];
            scanf("%d%s",&as,s1);
            ins(s1,as);
        }
        getchar();
        scanf("%s",s);
        trans();
        int id=1,pos=0;
        REP(i,0,tmp-1){
            if(id<=8){
                id++;
                if(ss[i]=='1') pos++; 
            }    
            else{
                if((pos%2==0&&ss[i]=='1') || (pos%2==1&&ss[i]=='0')){
                    for(int j=i-8;j<=i-1;j++){
                        s[tmp1++]=ss[j];
                    }
                }
                id=1;pos=0;
            }
        }
        Query(s);
        REP(i,1,cnt){
            printf("%c",ans1[i]);
        }
        printf("\n");
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/LMissher/p/9610840.html
今日推荐