HDU5237——模拟——Base64

Problem Description

Mike does not want others to view his messages, so he find a encode method Base64.

Here is an example of the note in Chinese Passport.

The Ministry of Foreign Affairs of the People‘s Republic of China requests all civil and military authorities of foreign countries to allow the bearer of this passport to pass freely and afford assistance in case of need.

When encoded by \texttt{Base64}, it looks as follows

VGhlIE1pbmlzdHJ5IG9mIEZvcmVpZ24gQWZmYWlycyBvZiB0aGUgUGVvcGxlJ3MgUmVwdWJsaWMgb2Yg
Q2hpbmEgcmVxdWVzdHMgYWxsIGNpdmlsIGFuZCBtaWxpdGFyeSBhdXRob3JpdGllcyBvZiBmb3JlaWdu
IGNvdW50cmllcyB0byBhbGxvdyB0aGUgYmVhcmVyIG9mIHRoaXMgcGFzc3BvcnQgdG8gcGFzcyBmcmVl
bHkgYW5kIGFmZm9yZCBhc3Npc3RhbmNlIGluIGNhc2Ugb2YgbmVlZC4=

In the above text, the encoded result of \texttt{The} is \texttt{VGhl}. Encoded in ASCII, the characters \texttt{T}, \texttt{h}, and \texttt{e} are stored as the bytes 84, 104, and 101, which are the 8-bit binary values 01010100, 01101000, and 01100101. These three values are joined together into a 24-bit string, producing 010101000110100001100101.
Groups of 6 bits (6 bits have a maximum of 26=64 different binary values) are converted into individual numbers from left to right (in this case, there are four numbers in a 24-bit string), which are then converted into their corresponding Base64 encoded characters. The Base64 index table is

0123456789012345678901234567890123456789012345678901234567890123
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

In the above example, the string 010101000110100001100101 is divided into four parts 010101, 000110, 100001 and 100101, and converted into integers 21,6,33 and 37. Then we find them in the table, and get V, G, h, l.

When the number of bytes to encode is not divisible by three (that is, if there are only one or two bytes of input for the last 24-bit block), then the following action is performed:

Add extra bytes with value zero so there are three bytes, and perform the conversion to base64. If there was only one significant input byte, only the first two base64 digits are picked (12 bits), and if there were two significant input bytes, the first three base64 digits are picked (18 bits). ‘=‘ characters are added to make the last block contain four base64 characters.

As a result, when the last group contains one bytes, the four least significant bits of the final 6-bit block are set to zero; and when the last group contains two bytes, the two least significant bits of the final 6-bit block are set to zero.

For example, base64(A) = QQ==, base64(AA) = QUE=.

Now, Mike want you to help him encode a string for k times. Can you help him?

For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.

Input

The first line contains an integer T(T≤20) denoting the number of test cases.
  
  In the following T lines, each line contains a case. In each case, there is a number k(1≤k≤5) and a string s. s only contains characters whose ASCII value are from 33 to 126(all visible characters). The length of s is no larger than 100.

Output

For each test case, output Case #t:, to represent this is t-th case. And then output the encoded string.

Sample Input

2
1 Mike
4 Mike

Sample Output

Case #1: TWlrZQ==
Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==

Source

The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest

Recommend

We have carefully selected several similar problems for you:  5245 5244 5243 5242 5241

大意:第二个样例卡了1天~~~先考虑被3整除的,在考虑余数,被3整除的块,先转化成24个二进制数,分成4块,转化成6进制数,对于s2,进行字符化,余数,为1的现在有8个字节,为了使得被6整除,后面加4个零,现在有两个字符,为了保证4个字符,后面再加两个‘=’,如果余数为2的话有16个字节,为了使得被6整除,后面加上2个零,变成18,现在有三个字符,后面加上一个‘=’,注意读入用gets,并且不能吃进第一个‘ ’,还有坑点是s转化之后是三个变四个,如果用同一个的话会把第四个给更新掉,就不是原来的字符串了,先保存到另一个字符串里面,再strcpy进去

渣渣代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int T,k;
char s[1100];
char s3[1100];
char s2[50];
int a[110];
char s1[100] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int n,x,y;
int flag,num;
void base()
{
    flag = 1;
    memset(s2,0,sizeof(s2));
    n = strlen(s+1);
    //puts(s);
   // printf("%d\n",n);
         x = n%3;
         y = n/3;
        s2[1] = s[3*y+1],s2[2] = s[3*y+2];
        // printf("%d %d \n",x,y);
        for(int i = 1 ; i <= 3*y; i++){
             num = s[i] ;
            if(i%3 == 1){
            for(int i = 8; i >= 1; i--){
                a[i] = num%2;
                num/= 2;
            }
         }
            else if(i%3 == 2){
                for(int i = 16; i >= 9; i--){
                a[i] = num%2;
                num/= 2;
                }
            }
            else if(i%3 == 0){
                for(int i = 24; i >= 17; i--){
                a[i] = num%2;
                num/= 2;
                }
               // for(int i = 1; i <= 24; i++)
               //     printf("%d",a[i]);
                num = 0;
                for(int i = 1; i <= 24; i++){
                    if(i%6 == 0){
                        num = num * 2 + a[i];
                        s3[flag++] = s1[num];
                        num = 0;
                    }
                    else {
                        num = num * 2 + a[i];
                    }
                }
            }
        }
       // printf("%d",flag);
       // puts(s+1);
        if(x == 1){
            for(int i = 12; i >= 9; i--)
                a[i] = 0;
            num = s2[1];
           // printf("%d\n",num);
            for(int i = 8; i >= 1; i--){
                a[i] = num%2;
                num/= 2;
            }
         //   for(int i = 1; i <= 8; i++)
         //      printf("%d",a[i]);
        num = 0;
        for(int i = 1; i <= 12; i++){
            if(i%6 == 0 ){
                num = num*2 + a[i];
               // printf("%d\n",num);
                s3[flag++] = s1[num];
               // printf("%c",s[1]);
                num = 0;
            }
            else
                num = num*2 + a[i];
        }
        s3[flag++] = ‘=‘;
        s3[flag++] = ‘=‘;
    }
        else if(x == 2){
            for(int i = 18; i >= 17; i--)
                a[i] = 0;
            num = s2[2];
            for(int i = 16; i >= 9; i--){
                a[i] = num%2;
                num/= 2;
            }
            num = s2[1] ;
            // printf("%c\n",s2[1]);
            //printf("%c\n",s2[2]);
            for(int i = 8; i >= 1; i--){
                a[i] = num%2;
                num/=2;
            }
            num = 0;
            for(int i = 1; i <= 18; i++){
                if(i%6 == 0 ){
                 num = num*2 + a[i];
                 s3[flag++] = s1[num];
                 num = 0;
                }
                else
                    num = num * 2 + a[i];
                }
            s3[flag++] = ‘=‘;
            }
      //  puts(s3+1);
        strcpy(s+1,s3+1);
      //  puts(s+1);
        memset(s3,0,sizeof(s3));
}
int main()
{
    int n,num;
    scanf("%d",&T);
    for(int cas = 1; cas <= T; cas++){
        memset(s,0,sizeof(s));
        memset(s2,0,sizeof(s2));
        scanf("%d",&k);
        getchar();
        gets(s+1);
        //puts(s+1);
        for(int i = 1; i <= k;i++){
         //   puts(s+1);
            base();
        }
        printf("Case #%d: ",cas);
        for(int i = 1; i < flag ; i++)
            printf("%c",s[i]);
        puts("");
    }
    return 0;
}

  

时间: 2024-10-16 10:42:52

HDU5237——模拟——Base64的相关文章

hdu 5237 Base64(模拟)

Base64 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1255    Accepted Submission(s): 564 Problem Description Mike does not want others to view his messages, so he find a encode method Base64.

Python——新浪微博爬虫之模拟登陆

在编写微博爬虫的过程中,免不了要进行模拟登录,因为新浪微博不登陆只能访问少量的微博信息. 然而,由于新浪微博的反爬虫功能在不断更新,例如改变了密码的加密算法(RSA),以前的一些模拟登陆方式已经不适用了.所以一开始试了好几种方法,均不能成功.后来受http://www.jb51.net/article/46053.htm启发,已经实现了. 目前,亲测能用的步骤是:①通过预登录,使用GET方法,获得登录所需的servertime, nonce, pubkey, rsakv:②使用encode64加

通过C#的HttpClient模拟提交form()表单

post提交表单一般无非是一般text文本和文件类型,如下 <input type="file"/> <input type="text"/> 如果模拟post提交表单的过程,该怎么做呢 这里就需要用到HttpClietn类 首先我们需要一个类去包装这些需要上载的数据,例如 /// <summary> /// 包装Data数据的Model /// </summary> public class SendData { /

在Python中用Request库模拟登录(二):博客园(简单加密,无验证码)

源代码分析 博客园的登录页面非常简单,查看网页源代码,可以发现两个输入框的id分别为input1.input2,复选框的id为remember_me,登录按钮的id为signin. 还有一段JavaScript代码,下面来简单分析一下. 先来看$(function(){});函数: 1 $(function () { 2 $('#signin').bind('click', function () { 3 signin_go(); 4 }).val('登 录'); 5 }); $(functio

python RSA加密解密及模拟登录cnblog

1.公开密钥加密 又称非对称加密,需要一对密钥,一个是私人密钥,另一个则是公开密钥.公钥加密的只能私钥解密,用于加密客户上传数据.私钥加密的数据,公钥可以解密,主要用于数字签名.详细介绍可参见维基百科. 2.RSA加密算法 RSA加密属于非对称加密.RSA算法基于一个十分简单的数论事实:将两个大质数相乘十分容易,但是想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥.维基百科中对RSA算法的安全性进行说明:RSA加密算法 "对极大整数做因式分解的难度决定了RSA算法的可靠性.换言

全程模拟新浪微博登录(2015)

很久之前就了解过模拟登录的过程,最近对python用的比较多,想来练练手,就想实现一下新浪微博登录,首先随便一搜,网上有大量的前辈们都做过了,我也仔细看了一下,并且参考之后发现无法登录,而且还有很多细节没有说得太清楚.同时网上最新的也是很久之前的,对于最新的版本也有一些改动,因此将我接近两天时间的研究全过程记录一下. 已有实现的简要过程 网上已有实现可以见http://www.douban.com/note/201767245/以及http://www.jb51.net/article/4477

vue+mockjs 模拟数据,实现前后端分离开发

在项目中尝试了mockjs,mock数据,实现前后端分离开发. 关于mockjs,官网描述的是 1.前后端分离 2.不需要修改既有代码,就可以拦截 Ajax 请求,返回模拟的响应数据. 3.数据类型丰富 4.通过随机数据,模拟各种场景. 等等优点. 总结起来就是在后端接口没有开发完成之前,前端可以用已有的接口文档,在真实的请求上拦截ajax,并根据mockjs的mock数据的规则,模拟真实接口返回的数据,并将随机的模拟数据返回参与相应的数据交互处理,这样真正实现了前后台的分离开发. 与以往的自己

Java 模拟新浪登录 2016

想学习一下网络爬虫.涉及到模拟登录,查阅了一番资料以后发现大部分都有点过时了,就使用前辈们给的经验,Firefox抓包调试,採用httpclient模拟了一下新浪登录. 不正确之处多多包括.须要的能够用浏览器调试看看还有哪些须要改动的,改改就能够了. 新浪登录认证流程: 1.预登陆获取pubkey/nonce/rsak等用于加密用户信息(get). 返回json 2.login.php?client=ssologin对用户账号进行加密username採用base64加密,password採用rs

爬虫实例(三):模拟登陆新浪

1.在模拟登陆的过程中第一步需要得到登陆前信息,用户名和密码通过js预先加密,所以必须要先将js预先加密的servertime和nonce和pubkey得到,下面json模块和re得到预先加密的信息 1 #---coding:utf-8--- 2 import urllib2 3 import re 4 import json 5 def get_servertime(): 6 url="http://login.sina.com.cn/sso/prelogin.php?entry=weibo&