poj Permutation Code (模拟)

来源:互联网 发布:sql 时间转换 编辑:程序博客网 时间:2024/05/23 05:07

题意:给定S,P,C三个字符串,C是加密好的字符串,根据加密原理,求解原文。

 

posS,为字符在S串中的位置,posP为字符在P串中的位置。

突破口在d,相当于密钥。

然后往前循环依次得出M串的每个字符。得出在P串的位置利用了两个相同的树异或值为0的原理。

 

代码:

 

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int MAX_ = 100;const int aciiMAX_ = 256;int posS[aciiMAX_], posP[aciiMAX_];char S[MAX_], P[MAX_], C[MAX_], M[MAX_];int main(){    int x, d, len, poS,poP, tmp,poC;    while(~scanf("%d",&x),x){        scanf("%s%s%s",S,P,C);        for(int i = 0; S[i]; i++){            posS[(int)S[i]] = i;        }        for(int i = 0; P[i]; i++){            posP[(int)P[i]] = i;        }        len = strlen(C);        d = (int)(pow(len,1.5) + x)%len;        tmp = d;        poS = posS[C[d]];        M[d] = P[poS];        tmp = (tmp - 1 + len) % len;        while(tmp != d){            poS = posS[M[(tmp + 1)%len]];            poC = posS[C[tmp]];            M[tmp] = P[poC ^ poS];            tmp = (tmp - 1 + len) % len;        }        M[len] = '\0';        printf("%s\n",M);    }    return 0;}


 

原创粉丝点击