xtu read problem training 2 B - In 7-bit
In 7-bit
This 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 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
Source
Author
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
char str[];
int main(){
int t,i,len;
scanf("%d",&t);
getchar();
while(t--){
gets(str);
len = strlen(str);
if(!len){puts("");continue;}
while(len){
int temp = len&;
len >>= ;
if(len) temp |= ;
if(temp < ) printf("");
printf("%X",temp);
}
for(i = ; str[i]; i++)
printf("%X",str[i]);
printf("\n");
}
return ;
}
最新文章
- 数据量大的数据转换成jason并显示在页面上
- DataGridView 在下拉框添加下来事件
- UVA - 1625 Color Length[序列DP 代价计算技巧]
- python之读取cdv
- 【JavaScript基础学习】关于正则表达式的完整内容
- 使用虚幻引擎中的C++导论(三-反射系统与迭代器)
- C读取文件
- Number of 1 Bits——LeetCode
- mysql 存储过程 游标的使用 与定义
- Quartz CronTrigger应用
- Date 和 SimpleDateFormat 类表示时间
- Xcode 7.0 官方免费的真机开发
- SpringMvc+Spring+MyBatis 基于注解整合
- Linux ELF 文件格式
- 让一个继承unittest.TestCase的类下的setUp和tearDown只执行一次
- smarty 模板标签
- Circular reference 循环引用 第二联系人
- 各大浏览器相继发布声明将停止支持 TLS 1.0 和 TLS 1.1 !
- HTML5 audio标签自制音乐播放器
- (转)Linux系统sersync数据实时同步