In 7-bit zoj3713 (位运算+进制转换,水)

来源:互联网 发布:用友重装后数据恢复 编辑:程序博客网 时间:2024/05/29 17:52

ZOJ Problem Set - 3713
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

342yukkuri 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

0234321779756B6B75726920736869746569747465206E652121219A0168747470733A2F2F656E2E77696B6970656469612E6F72672F77696B692F416E737765725F746F5F4C6966652C5F7468655F556E6976657273652C5F616E645F45766572797468


ps:样例显示可能不太正常

题意:

给你n个串

按照16进制输出某些内容

1.先把字符串长度化为2进制

2. 然后取2进制后7位 如果2进制串大于7位 那么“最高位” (也可以理解为标记位见题目红字)置为1 那么答案会是8位2进制串 打印为16进制 继续取7位 重复判断

如果小于7位 那么直接打印16进制出来 结束

3.最后把输入串的ascII码按照16进制(都是两位的)打印出来


重要操作 在2

其实不用把长度转化为2进制 直接按照10进制进行操作

1.取后7位 即为10进制对128取余

2.判断2进制是否大于7位 即为10进制除以128是否大于1

3.最高位(标记位) 为第8位,置为1 即为10进制+128

也就是位运算与10进制的关系

此外16进制可以直接%02X打印 (X大写出来的字母也是大写反之亦然)

8进制也可以 %O打印

其他进制自己手写了0.0


#include <algorithm>#include <iostream>#include <cstring>#include <iomanip>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>using namespace std;#define For(i,a,b) for(i=a;i<=b;i++)#define _For(i,a,b) for(i=b;i>=a;i--)#define Out(x) cout<<x<<endl#define Outdouble(x,a) cout<<fixed<<setprecision(a)<<1.0*x<<endl#define pf printf#define sf scanf#define mset(arr,num) memset(arr,num,sizeof(arr))#define ll long longconst ll inf = 3000010; ///#define ok std::ios::sync_with_stdio(0)#pragma comment(linker, "/STACK:102400000,102400000")// #define debug#if defined (debug)---check---#endif/// ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^ ///char s[inf];int main(){    int n,len;    int temp,i;    scanf("%d",&n);    getchar();    while(n--)    {        gets(s);        len = strlen(s);        if(len == 0)   ///特判0 空串        {            printf("00\n");            continue;        }        while(len)        {            temp = len%128;  ///取后7位的操作            len=len/128;            if(len)          ///判断目前的2进制串是否大于7位            {                temp+=128;  ///标记位置为1的操作            }            printf("%02X",temp);        }        for (i = 0; s[i] ; i++)        {            printf("%02X",s[i]);        }        printf("\n");    }    return 0;}


1 0
原创粉丝点击