ZOJ 3713 In 7-bit

7位编码与ASCII输出
本文介绍了一种使用7位编码存储字符串长度的方法,并通过示例代码展示了如何将字符串转换为带长度前缀的十六进制ASCII表示形式。此方法适用于短字符串,在编程竞赛等场景中有实际应用。
In 7-bit

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Very often, especially in programming contests, we treat a sequence of non-whitespace characters as a string. But sometimes, a string may contain whitespace characters or even be empty. We can have such strings quoted and escaped to handle these cases. However, a different approach is putting the length of the string before it. As most strings are short in practice, it would be a waste of space to encode the length as a 64-bit unsigned integer or add a extra separator between the length and the string. That's why a 7-bit encoded integer is introduced here.

To store the string length by 7-bit encoding, we should regard the length as a binary integer. It should be written out by seven bits at a time, starting with the seven least-significant (i.e. 7 rightmost) bits. The highest (i.e. leftmost) bit of a byte indicates whether there are more bytes to be written after this one. If the integer fits in seven bits, it takes only one byte of space. If the integer does not fit in seven bits, the highest bit is set to 1 on the first byte and written out. The integer is then shifted by seven bits and the next byte is written. This process is repeated until the entire integer has been written.

With the help of 7-bit encoded integer, we can store each string as a length-prefixed string by concatenating its 7-bit encoded length and its raw content (i.e. the original string).

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases.

Each test case is simply a string in a single line with at most 3000000 characters.

Output

For each test case, output the corresponding length-prefixed string in uppercase hexadecimal. See sample for more details.

Sample Input
3
42
yukkuri shiteitte ne!!!
https://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe_and_Everything_.2842.29
Sample Output
023432
1779756B6B75726920736869746569747465206E65212121
9A0168747470733A2F2F656E2E77696B6970656469612E6F72672F77696B692F416E737765725F746F5F4C6966652C5F7468655F556E6976657273652C5F616E645F45766572797468696E6723416E737765725F746F5F7468655F556C74696D6174655F5175657374696F6E5F6F665F4C6966652E32435F7468655F556E6976657273655F616E645F45766572797468696E675F2E323834322E3239

Author: WU, Zejun
Contest: The 10th Zhejiang Provincial Collegiate Programming Contest

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 
 5 const int maxn = 3000005;
 6 char str[maxn];
 7 int main()
 8 {
 9     int ca, len, lc;
10     scanf("%d", &ca);
11     getchar();
12     while (ca--)
13     {
14         gets(str);
15         lc = len = strlen(str);
16         for (;;){
17             printf("%02X", (lc & 127) + (lc > 127 ? 128 : 0));
18             lc = lc >> 7;
19             if (!lc) break;
20         }
21         for (int i = 0; i < len; i++)
22             printf("%02X", (int)str[i]);
23         printf("\n");
24     }

 

    给定一个字符串,要求输出其中的字符的ASCAII码值的十六进制数,并在前面添加表示字符串的长度的编码,这个编码是一组字节,它的定义是,先长度值的二进制数的后七位存放在一个字节中,如果这七位不足以表达长度值(即二进制数第七位左边还有1),则将这个字节的最高位置为1,将长度值的二进制数右移七位。重复这样的操作,直到这个二进制变为0。最后按顺序把这些字节以两位的十六进制数输出。

 

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 
 5 const int maxn = 3000005;
 6 char str[maxn];
 7 int main()
 8 {
 9     int ca, len, lc;
10     scanf("%d", &ca);
11     getchar();
12     while (ca--)
13     {
14         gets(str);
15         lc = len = strlen(str);
16         for (;;){
17             printf("%02X", (lc & 127) + (lc > 127 ? 128 : 0));
18             lc = lc >> 7;
19             if (!lc) break;
20         }
21         for (int i = 0; i < len; i++)
22             printf("%02X", (int)str[i]);
23         printf("\n");
24     }
25 }

 

转载于:https://www.cnblogs.com/cumulonimbus/p/5465891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值