xtu1143 Data Encoding(huffman)
Data Encoding |
||
Accepted : 14 | Submit : 118 | |
Time Limit : 1000 MS | Memory Limit : 1048576 KB |
Data EncodingGiven a string S, your job is to encode it into a new string T using given charset C. To make the decoding easy, each original character X in S is encoded into a string Y made up of characters in C. What's more, for two different X1 and X2, Y1 must be different from Y2 and all its non-empty prefixes. What's the minimum possible length of T? InputThere are multiple test cases. Each test case contains two lines, S and C. The length of S will be at most 100000. The length of C will be at least 2 and all characters in C will be different. OutputFor each test case, output the minimum possible length of T. Sample Inputtest
01
1267650600228229401496703205376
ATCG
Sample Output6
50
|
题意:给定你一个原串s1,现在要用s2编码。
分析:如果s2的长度为2就是就是经典的huffman编码了,这里其实也一样,由原来的2叉变为k叉。
注意:输入我cin读入字符串 WA,后改用gets AC了,中间可能会有空格。
还有一点就是,有可能叶子节点不能满k叉,所以要加以注意。

1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define N 100010 5 6 using namespace std; 7 8 char s1[N],s2[N]; 9 int num[N]; 10 11 int huffman(int n,int k) 12 { 13 int i,j,sum,ans=0; 14 if(n<=k) 15 { 16 for(i=0;i<n;i++) 17 ans+=num[i]; 18 return ans; 19 } 20 int m=(n-1)%(k-1); 21 if(m) 22 { 23 sum=0; 24 for(i=0;i<=m;i++) 25 { 26 sum+=num[i]; 27 } 28 ans+=sum; 29 for(i=m+1;i<n;i++) 30 { 31 if(sum>num[i]) 32 num[i-1]=num[i]; 33 else break; 34 } 35 num[i-1]=sum; 36 } 37 for(i=m;i<n-1;i=i+k-1) 38 { 39 sum=0; 40 for(j=i;j<i+k&&j<n;j++) 41 { 42 sum+=num[j]; 43 } 44 ans+=sum; 45 for(j=i+k;j<n;j++) 46 { 47 if(sum>num[j]) 48 num[j-1]=num[j]; 49 else break; 50 } 51 num[j-1]=sum; 52 } 53 return ans; 54 } 55 56 int main() 57 { 58 int i,k,len1,len2; 59 while(getline(s1))//gets()读一行 cin输入WA 错误不明!! 60 { 61 getline(s2); 62 k=strlen(s2); 63 len2=strlen(s1); 64 sort(s1,s1+len2); 65 len1=0; 66 for(i=0;i<len2;i++) 67 { 68 num[len1]=1; 69 while(s1[i]==s1[i+1]) 70 { 71 num[len1]++; 72 i++; 73 } 74 len1++; 75 } 76 sort(num,num+len1); 77 cout<<huffman(len1,k)<<endl; 78 } 79 return 0; 80 } 81 /* 82 test 83 01 84 85 1267650600228229401496703205376 86 ATCG 87 88 12233444 89 123 90 91 123456 123 92 93 */