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!!!
the_Universe,_and_Everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe_and_Everything.2842.29”>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
题目链接:ZOJ-3713
题目思路:这道题只要是题意很难懂,orz,看不懂去搜了题解的
给定一个字符串,按照十六进制输出。
1. 用给定方式输出字符串长度
① 将长度len转换成二进制,取后七位,如果除去后七位前边还有1那么就在第八位位置加上1
②然后将len右移7位
③继续上述步骤
④最后输出字符串转十六进制
例如: 10001000100
那么第一次取出来的后七位就是1000100,因为前边还有1,所以第一次取出来的变为11000100,然后将len右移7位得到1000,依次输出他们的十六进制就可以了
ps: 取x的后k位操作 : x & ((1 << k) - 1)
以下是代码:
#include <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof (a))
#define FOR(i,n) for (int i = 0; i < n; i++)
#define INF 1e9
#define eps 1e-10
using namespace std;
typedef long long ll;
string s;
int main(){
int t;
cin >> t;
getchar();
while(t--)
{
getline(cin,s);
int l = s.size();
while(1)
{
int temp = l & ((1 << 7) - 1) ; // 取后 7 位
if (l > 127) temp += 128; //如果出去后7位前面还有1,则第八位+1即 数值加上128
printf("%02X",temp); //注意所有16进制数都必须为两位!
l >>= 7; //然后l右移7位
if (l == 0) break;
}
for (int i = 0; i < s.size(); i++) printf("%02X",(int)s[i]); //最后输出字符串转十六进制
cout << endl;
}
return 0;
}