Stay Hungry,Stay Foolish!

1236. Decoding Task

1236. Decoding Task

https://acm.timus.ru/problem.aspx?space=1&num=1236

 

思路

  1. 对于带空格串加密结果,第一个字符61,K1K2 XOR 32 = 61,可以算出K1K2
  2. 对于原始串加密结果,第一个字符05, K1K2 XOR C1C2 = 05, 可以算出C1C2
  3. 对于带空格串加密结果,第二个字符07, K3K4 XOR C1C2 = 07, 可以算出 K3K4
  4. 对于原始串加密结果,第二个字符26, K3K4 XOR C3C4 = 26, 可以算出C3C4
  5. 对于带空格串加密结果,第三个字符28, K5K6 XOR C3C4 = 28, 可以算出 K3K4
  6. ...
  7. ...
  8. ...

 

 

Code

https://blog.csdn.net/qq_38231051/article/details/82392330

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char a[20004];
char b[20004];
char c[20004];
 
int ten(int i,char* x)
{
    int k;
    if(x[2*i]>='A'){
        k = (10+x[2*i]-'A')*16;
    }else
        k = (x[2*i]-'0')*16;
    if(x[2*i+1]>='A'){
        k += (10+x[2*i+1]-'A');
    }else
        k += (x[2*i+1]-'0');
    return k;
}
 
void setnum(int first,int end,int i)
{
    if(first>=10){
        c[2*i] = 'A'+first-10;
    }else
        c[2*i] = '0'+first;
    if(end>=10){
        c[2*i+1] = 'A'+end-10;
    }
    else
        c[2*i+1] = '0'+end;
 
}
 
void getit(int len)
{
    int first,end;
    int i=0,j=0;
    int temp=32;
    int result;
    int A=0;
    A = ten(0,b);
    result = A^temp;
    first = result/16;
    end = result%16;
    setnum(first,end,i);
    ++i;
    int k=0;
    for(k=0;k<len/2;++k)
    {
        temp = result^ten(k,a);
        A = ten(k+1,b);
        result = A^temp;
        first = result/16;
        end = result%16;
        setnum(first,end,k+1);
        ++i;
    }
    c[(k+1)*2]='\0';
 
}
 
int main()
{
    int temp,len;
    while(scanf("%s %s",a,b)!=EOF){
        getit(strlen(a));
        printf("%s\n",c);
    }
}

 

https://blog.csdn.net/worldvagrant/article/details/108608292

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 20006
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char a[M],b[M],c[M];
int ans[M];
int len;

int cread(char x){    16进制转换为10进制
    if(x >= 'A'){
        return x-'A'+10;
    }
    if(x >= '0' && x <= '9'){
        return x-'0';
    }
}

void todec(char *x){    先将第一个字符串转换为十进制
    for(int i = 0; i < len; i += 2){
        ans[i/2] = cread(x[i])*16 + cread(x[i+1]);
    }
}

int main(int argc, char *argv[]) {
    while(scanf("%s%s",a,b) != EOF){
        len = strlen(a);
        todec(a);
        int ex = 32, cnt = 0;
        for(int i = 0; i < len+2; i += 2){
            int x = cread(b[i]), y = cread(b[i+1]);
            x = x*16+y;
            ex = ex^x;
            printf("%02X", ex);
            ex = ans[cnt++]^ex;
        }
        printf("\n");
    }
}

 

异或操作群

如果 a ^ b =c

那么

a ^ c = b

b ^ c = a

可以从集合论角度理解

 https://www.cnblogs.com/organic/p/5023038.html

3、集合的异或运算在计算机中的应用
3.1 证明:当 AΔB = C 时,AΔC = B

即典型的异或运算 A xor B = C,A xor C = B
在编程中常用该可逆运算对数据 B 异或一个常量 A 转换后进行加密保护,当要还原数据B时,再次用 C 异或常量 A 即可得到B

image

 

posted @ 2022-12-10 14:45  lightsong  阅读(24)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel