Xtu read problem training 2 B-in 7-bit

Source: Internet
Author: User
In 7-bittime limit: 2000 msmemory limit: 65536 kbthis problem will be judged on zju. Original ID: 3713
64-bit integer Io format: % LLD Java class name: Main

Very often, especially in programming contests, we treat a sequence of non-whitespace characters as a string. but sometimes, a string may contain in 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 wocould 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 shoshould regard the length as a binary integer. it shoshould 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
0234321779756B6B75726920736869746569747465206E652121219A0168747470733A2F2F656E2E77696B6970656469612E6F72672F77696B692F416E737765725F746F5F4C6966652C5F7468655F556E6976657273652C5F616E645F45766572797468696E6723416E737765725F746F5F7468655F556C74696D6174655F5175657374696F6E5F6F665F4C6966652E32435F7468655F556E6976657273655F616E645F45766572797468696E675F2E323834322E3239
Sourcethe 10th Zhejiang Provincial Collegiate Programming contestauthorwu, zejun solution: encode the length of the output string first. Yes. Seven binary digits are output at a time, but the numbers represented by these seven binary digits are represented in hexadecimal notation. After the length is expressed as binary, if there is a number after the 7-bit binary is split, one is output before the 7-bit binary, output 0 before the seven binary digits. the output character is followed by a hexadecimal number for each character.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 char str[3000100];16 int main(){17     int t,i,len;18     scanf("%d",&t);19     getchar();20     while(t--){21         gets(str);22         len = strlen(str);23         if(!len){puts("00");continue;}24         while(len){25             int temp = len&127;26             len >>= 7;27             if(len) temp |= 128;28             if(temp < 16) printf("0");29             printf("%X",temp);30         }31         for(i = 0; str[i]; i++)32             printf("%X",str[i]);33         printf("\n");34     }35     return 0;36 }
View code

 

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.