In 7-bit ZOJ - 3713(进制操作转换)

7位编码字符串长度
本文介绍了一种使用7位编码来存储字符串长度的方法,适用于短字符串,并提供了编码过程的详细说明及示例输入输出。


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
 

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[3000003];
int main(){
    int t;
    cin >> t;
    getchar();
    while(t--){
        gets(a);
        int len = strlen(a);
        int l = len;
        if(!l){
            printf("00\n");
        }
        else{
            while(l){
                int tmp = l % 128;//取后七位数字
                l /= 128;//右移七位
                if(l){
                    tmp += 128;//第八位上变成1
                }
                printf("%02X",tmp);
            }
            int i;
            for(i = 0; i < len; i++){
                printf("%02X",a[i]);
            }
            puts("");
        }
    }
    return 0;
}


### 关于ZOJ 2723 Semi-Prime问题的解释 #### 定义与理解 半素数定义为可以被分解成两个质数乘积的大于一的整数[^1]。例如,6 是一个半素数因为它能表示为 \(2 \times 3\);而像 12 这样的数字则不是因为其有超过两组不同的质因数组合。 #### 解决方案概述 考虑到题目给定的数据范围较大 (2<N<1000,000),对于每一个输入单独进行因式分解将会非常耗时并可能导致时间超出限制[^3]。因此,更有效的策略是在程序启动之初就预先计算好所有的可能解,并存储起来供后续查询使用。 #### 实现方法 为了高效处理这个问题,建议采用埃拉托斯特尼筛法来找出指定区间内的所有质数。接着遍历这些质数列表,通过相乘的方式构建出所有符合条件的半素数集合。最后,在面对具体询问时只需快速查找预存的结果即可得到答案。 ```cpp #include <iostream> #include <vector> using namespace std; const int MAX_N = 1e6; bool is_prime[MAX_N]; vector<int> primes; void sieve() { fill(is_prime, is_prime + MAX_N, true); is_prime[0] = is_prime[1] = false; for (int i = 2; i * i < MAX_N; ++i) { if (!is_prime[i]) continue; for (int j = i * i; j < MAX_N; j += i) is_prime[j] = false; } } // Collect all the prime numbers up to sqrt(MAX_N). void collect_primes() { for (int i = 2; i * i <= MAX_N; ++i) if (is_prime[i]) primes.push_back(i); } // Precompute all semi-prime numbers. vector<bool> precompute_semi_primes() { vector<bool> semi_primes(MAX_N, false); for (size_t i = 0; i < primes.size(); ++i) { for (size_t j = i; j < primes.size() && primes[i]*primes[j] < MAX_N; ++j) { semi_primes[primes[i] * primes[j]] = true; } } return semi_primes; } int main(){ sieve(); collect_primes(); auto semi_primes = precompute_semi_primes(); int n; while(cin >> n){ cout << (semi_primes[n]? "Yes\n":"No\n"); } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值