HDU-2527 safe or unsafe

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2527

Create a user-defined tree and return the WPL value.

Safe or unsafe

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 1511 accepted submission (s): 594

Problem descriptionjavac ++ saw an interesting thing while reading books on the computer one day! Each string of characters can be encoded into numbers to store information, but the storage space obtained by different encoding methods is different! It is insecure when the storage space exceeds a certain value! So javac ++ wants to see if there is one way to get the space value with the smallest character encoding! Obviously, this is okay, because the book contains this part of content-Huffman coding (Huffman coding); the weight of a letter is equal to the frequency at which the letter appears in the string. So javac ++ wants you to help me to give you a security value and a string, and let you determine whether the string is safe? There are multiple groups of input cases. First, a number N indicates that there are N groups of data, and then each group of data has a value m (integer ), there is no space in a string and it can only contain lowercase letters! Output: If the encoding value of the string is less than or equal to the given value, yes is output; otherwise, no is output. Sample input212helloworld66ithinkyoucandoit sample outputnoyes
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 using namespace STD; 5 struct node 6 {7 char ch; // The Node value is 8 int weight; // weight 9 int parent; // parent node 10 int lchild, rchild; // child node 11} HT [20020]; 12 struct hcode 13 {14 char CD [10010]; // store the 01 encoded string. 15 int start; // The start of the length. 16} HCD [10010]; 17 void creat (node HT [], char * C, int * w, int N) // create a Harman tree. 18 {19 int I, S1, S2, K, min1, min2; 20 for (I = 1; I <= N; I ++) // initialize leaf node 21 {22 HT [I]. ch = C [I-1]; // character. 23 HT [I]. weight = W [I-1]; // weight 24 HT [I]. parent = HT [I]. lchild = HT [I]. rchild = 0; // The start value is 0. 25} 26 for (; I <2 * n; I ++) // There are 2 * n-1 nodes in the tree. 27 {28 HT [I]. parent = 0; 29 HT [I]. lchild = 0; 30 HT [I]. rchild = 0; 31} 32 for (I = n + 1; I <2 * n; I ++) 33 {34 min1 = min2 = 9999999; 35 For (k = 1; k <= I-1; k ++) // obtain the minimum two values. 36 IF (HT [K]. parent = 0) 37 {38 If (HT [K]. weight <min1) 39 {40 min2 = min1; 41 S2 = S1; 42 min1 = HT [K]. weight; 43 S1 = K; 44} 45 else if (HT [K]. weight <min2) 46 {47 min2 = HT [K]. weight; 48 S2 = K; 49} 50} 51 52 HT [S1]. parent = I; // create a node. 53 HT [s2]. parent = I; 54 HT [I]. lchild = S1; 55 HT [I]. rchild = S2; 56 HT [I]. weight = HT [S1]. weight + HT [s2]. weight; 57 58} 59 60} 61 void creathcode (node HT [], hcode HCD [], int N) // Harman encoding. 62 {63 int I, F, C; 64 hcode HC; 65 for (I = 1; I <= N; I ++) 66 {67 HC. start = N; 68 C = I; 69 F = HT [I]. parent; 70 while (F! = 0) 71 {72 If (HT [f]. lchild = C) 73 HC. CD [HC. start --] = '0'; 74 else 75 HC. CD [HC. start --] = '1'; 76 c = f; 77 F = HT [f]. parent; 78} 79 HC. start ++; 80 HCD [I] = HC; 81} 82} 83 int main () 84 {85 int T, M, I, R, J; 86 int W [200]; 87 char STR [200], C [200]; 88 scanf ("% d", & T); 89 while (t --) 90 {91 memset (W, 0, sizeof (w); 92 scanf ("% d", & M); 93 getchar (); 94 scanf ("% s ", str); 95 int Len = strlen (STR); 96 R = 0; 97 c [R ++] = STR [0]; 98 W [0] = 1; 99 for (I = 1; I <Len; I ++) 100 {101 For (j = 0; j <r; j ++) 102 {103 If (C [J] = STR [I]) 104 {105 W [J] = W [J] + 1; 106 break; 107} 108} 109 If (j = r) 110 {W [R] = 1; 111 C [R ++] = STR [I]; 112} 113} 114 C [R] = '\ 0'; 115 If (r = 1) 116 {117 If (W [0] <= m) 118 cout <"yes" <Endl; 119 else120 cout <"no" <Endl; 121 continue; 122} 123 creat (HT, C, W, R ); 124 creathcode (HT, HCD, R); 125 int WPL = 0; 126 for (I = 1; I <= r; I ++) 127 {128 WPL + = (R-HCD [I]. start + 1) * HT [I]. weight; 129 130} 131 If (WPL <= m) 132 cout <"yes" <Endl; 133 else134 cout <"no" <Endl; 135} 136 return 0; 137}

 

 

HDU-2527 safe or unsafe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.