标签:while push last 模板 print roo vector 生生 --
今天试着做了个Huffman,结果压缩算法被硬生生写成膨胀算法
我好蒻啊!!!!!!!!!!!!!!!
加密:
1 #include<bits/stdc++.h> 2 #include<windows.h> 3 #define ll long long 4 #define N 10000001 5 using namespace std; 6 int cnt; 7 struct node{ 8 int l,r,val; 9 char ch; 10 }hfm[N]; 11 12 struct cmp{bool operator()(int a,int b){return hfm[a].val>hfm[b].val;}}; 13 14 char a[128],b[128],str[N]; 15 int tohfm[256],len,root,cdl[256]; 16 bitset<8>code[256]; 17 bool apr[256]; 18 priority_queue<int,vector<int>,cmp>q; 19 20 void dfs(int x,int dep,int cd){ 21 if(hfm[x].ch){ 22 int ascii=(int)hfm[x].ch; 23 cdl[ascii]=dep; 24 for(int i=dep-1;i>=0;i--) 25 code[ascii][i]=cd&1,cd>>=1; 26 return; 27 } 28 dfs(hfm[x].l,dep+1,cd<<1); 29 dfs(hfm[x].r,dep+1,cd<<1|1); 30 } 31 32 void huffman(){ 33 for(int i=1;i<=cnt;i++)q.push(i); 34 while(q.size()>=2){ 35 int a,b,c; 36 a=q.top(),q.pop(); 37 b=q.top(),q.pop(); 38 c=++cnt; 39 hfm[c].l=a,hfm[c].r=b,hfm[c].val=hfm[a].val+hfm[b].val; 40 q.push(c); 41 } 42 dfs(root=cnt,0,0); 43 } 44 45 void Out(int x){ 46 char ch[16]={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘}; 47 printf("%c%c",ch[x/16],ch[x%16]); 48 } 49 50 int main(){ 51 scanf("%s%s",a,b); 52 freopen(a,"r",stdin); 53 freopen(b,"w",stdout); 54 while(~scanf("%c",&str[++len])){ 55 int ascii=(int)str[len]; 56 if(!apr[ascii]) 57 tohfm[ascii]=++cnt,apr[ascii]=true,hfm[cnt].ch=str[len]; 58 hfm[tohfm[ascii]].val++; 59 } 60 int cnt1=cnt; 61 huffman(); 62 Out(cnt1); 63 for(int i=1;i<=cnt1;i++){ 64 int ascii=(int)hfm[i].ch; 65 Out(ascii); 66 int ans=1; 67 for(int j=0;j<cdl[ascii];j++)ans=ans<<1|code[ascii][j]; 68 Out(ans); 69 } 70 int tail=0,ans=0; 71 for(int i=1;i<=len;i++){ 72 int ascii=(int)str[i]; 73 for(int j=0;j<cdl[ascii];j++){ 74 tail++; 75 ans=ans<<1|code[ascii][j]; 76 if(tail==7){ 77 Out(ans); 78 tail=0,ans=0; 79 } 80 } 81 } 82 if(tail) 83 Out(ans<<(7-tail)); 84 return 0; 85 }
解密:
1 #include<bits/stdc++.h> 2 #include<windows.h> 3 #include<conio.h> 4 #define ll long long 5 #define N 100001 6 using namespace std; 7 char hfm[1024],a[128],b[128]; 8 int n; 9 10 int In(){ 11 int re=0; 12 char a,b; 13 if(!~scanf("%c%c",&a,&b))return -1; 14 re=(a<=‘9‘?a-‘0‘:a-‘A‘+10); 15 re=re<<4|(b<=‘9‘?b-‘0‘:b-‘A‘+10); 16 return re; 17 } 18 19 int main(){ 20 scanf("%s%s",a,b); 21 freopen(a,"r",stdin); 22 freopen(b,"w",stdout); 23 n=In(); 24 for(int i=1;i<=n;i++){ 25 int ch,code; 26 ch=In(); 27 code=In(); 28 hfm[code]=(char)ch; 29 // printf("%c : %d\n",ch,code); 30 } 31 int byte,last; 32 int val=1; 33 while(~(byte=In())){ 34 for(int i=6;i>=0;i--){ 35 val=val<<1|((byte&(1<<i))>>i); 36 if(hfm[val]){ 37 if(hfm[val]&(1<<7)) 38 if(!last)last=hfm[val]; 39 else printf("%s",last<<8|(hfm[val])),last=0; 40 else printf("%c",hfm[val]); 41 val=1; 42 } 43 } 44 } 45 return 0; 46 }
BUG很多,可行性很低,我很蒻
但是可以用来加密(雾)
其实就是模板改了一下
标签:while push last 模板 print roo vector 生生 --
原文地址:https://www.cnblogs.com/nlKOG/p/9822383.html