*
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <string.h>
#define MAX 1000000
using namespace::std;
struct huffnode{
int weight;//权值
int lchild,rchild,parent;
}typedef huffnode;
void select(int len,int weight[],int &s1,int &s2,huffnode* &hufftree){
int min1=MAX;
int min2=MAX;
s1=-1;
s2=-1;
for(int i=0;i<len;i++){
if(hufftree[i].parent==-1){//没有父结点
if(hufftree[i].weight<min1){
s2=s1;
min2=min1;
s1=i;
min1=hufftree[i].weight;
}
else if(hufftree[i].weight<min2){
s2=i;
min2=hufftree[i].weight;
}
}
}
}
void HuffCode(int weight[],int n,huffnode* &hufftree,char** &code){
int m=2*n-1;//霍夫曼树结点总数
int s1,s2;//最小值、此小值
//初始化霍夫曼树
hufftree=(huffnode*)malloc(m*sizeof(huffnode));
for(int i=0;i<n;i++){
hufftree[i].lchild=-1;
hufftree[i].rchild=-1;
hufftree[i].parent=-1;
hufftree[i].weight=weight[i];
}
for(int i=n;i<m;i++){
hufftree[i].lchild=-1;
hufftree[i].rchild=-1;
hufftree[i].parent=-1;
hufftree[i].weight=0;
}
//构建霍夫曼树
for(int i=n;i<m;i++){
select(i,weight,s1,s2,hufftree);//从i的前面选取最小值和次小值
hufftree[s1].parent=i;
hufftree[s2].parent=i;
hufftree[i].lchild=s1;
hufftree[i].rchild=s2;
hufftree[i].weight=hufftree[s1].weight+hufftree[s2].weight;//构建新结点
}
//霍夫曼编码
code=(char**)malloc(n*sizeof(char*));//n个结点的霍夫曼编码地址
// char temp_code[n];//临时存储霍夫曼编码
char *temp_code=(char*)malloc(n*sizeof(char));
for(int i=0;i<n;i++){//从叶节点访问到根节点
int a,b;
int start=n;
temp_code[start]='\0';//字符串结尾
for(a=i,b=hufftree[a].parent; b!=-1 ;a=hufftree[a].parent,b=hufftree[b].parent){
if(a==hufftree[b].lchild){
temp_code[--start]='0';//左分支为0
}
else{
temp_code[--start]='1';//右分支为1
}
}
code[i]=(char *)malloc((n-start+1)*sizeof(char));//n个节点霍夫曼代码的值
strcpy(code[i],&temp_code[start]);
}
}
int main(){
int n,i,j,k;//叶子结点总数
char s[200];//存放字符
int weight[200];//存放权值
huffnode *hufftree;//创建结构体数组用来存储霍夫曼树
char **code;//存储霍夫曼编码
char ss[200];//用于转化哈夫曼编码的字符
scanf("%d\n",&n);
for(i=0;i<n;i++)
scanf("%c ",&s[i]);
s[n]='\0';//s是字符数组,不是字符串数组
for(i=0;i<n;i++)
scanf("%d",&weight[i]);
HuffCode(weight,n,hufftree,code);
fflush(stdin);//清空缓存区
gets(ss);
for(i=0;ss[i]!='\0';i++){
for(k=0;k<n;k++)
if(s[k]==ss[i])
break;
for(j=0;code[k][j]!='\0';j++)
cout<<code[k][j];
}
printf("\n");
puts(ss);
return 0;
}