POJ2015 Permutation Code
2013-07-14 19:33
127 查看
DescriptionAs the owner of a computer forensics company, you have just been given the following note by a new client: I, Albert Charles Montgomery, have just discovered the most amazing cypher for encrypting messages. Let me tell you about it. To begin, you will need to decide on a set of symbols, call it S, perhaps with the letters RATE. The size of this set must be a power of 2 and the order of the symbols in S is important. You must note that R is at position 0, A at 1, T at 2, and E at 3. Youwill also need one permutation P of all those symbols, say TEAR. Finally you will need an integer, call it x. Together, these make up the key. Given a key, you are now ready to convert a plaintext message M of length n (M[0], M[1]... M[n-1]), that has somebut not necessarily all of the symbols in S, into a cyphertext string C, also of length n (C[0], C[1],...C[n-1]), that has some but not necessarily all of the symbols in S. The encrypting algorithm computes C as follows: Calculate an integer d as the remainder after dividing the integer part of (n1.5 + x) by n. This can be expressed more succinctly as d = (int)(n1.5 + x) % n, where "%" is the remainder operator. Set C[d] to be the symbol in S whose position is the same as the position of M[d] in P. For each j != d in 0..n-1, set C[j] to be the symbol in S whose position is the value obtained by xor-ing the position of M[j] in P with the position of M[(j+1) % n] in S. Note that the bitwise xor operator is "^" in C, C++, and Java.For example, consider this scenario where S=RATE, P=TEAR, x=102, M=TEETER, and n=6. To compute d, first calculate 61.5 + 102 = 116.696938, then take the remainder after dividing by 6. So d = 116 % 6 = 2. The following table shows the steps infilling in the cyphertext C. Note that the order of the steps is not important.
I have included additional examples of encrypted messages at the end of this note for you to experiment with. However, first, I need to tell you about the decryption algorithm. Unfortunately, the next page of the note, with the decrypting algorithm, is completely unreadable because it is covered with huge, overlapping, messy ink blots. Given your considerable skill in unravelling puzzles, your task is to write the decoder based onyour knowledge of the encoding algorithm.InputThe input for the decoder consists of one or more sets of {key, encrypted message} pairs. The key is on 3 separate lines. The first line contains the single integer x, 0 < x < 10,000; the second line contains the string S; and the third line contains the stringP, which will be a permutation of S. The length of S (and therefore P) will always be one of the following powers of two: 2, 4, 8, 16, or 32. Following the key is a line containing the encrypted message string C, which will contain at least one and at mostsixty characters. The strings S, P, and C will not contain whitespace, but may contain printable characters other than letters and digits. The end of the input is a line which contains the single integer 0.OutputFor each input set print the decrypted string on a single line, as shown in the sample output.Sample Input
0 | 1 | 2 | 3 | 4 | 5 | ||
S = | R | A | T | E | |||
P = | T | E | A | R | |||
M = | T | E | E | T | E | R | |
C = | E | M[0] is T, T is at P[0]. M[1] is E, E is at S[3]. C[0] = S[0 xor 3] = S[3] | |||||
E | T | M[1] is E, E is at P[1]. M[2] is E, E is at S[3]. C[1] = S[1 xor 3] = S[2] | |||||
E | T | A | 2 is d. M[2] is E, E is at P[1], so C[2] = S[1] | ||||
E | T | A | E | M[3] is T, T is at P[0]. M[4] is E, E is at S[3]. C[3] = S[0 xor 3] = S[3] | |||
E | T | A | E | A | M[4] is E, E is at P[1]. M[5] is R, R is at S[0]. C[4] = S[1 xor 0] = S[1] | ||
E | T | A | E | A | A | M[5] is R, R is at P[3]. M[0] is T, T is at S[2]. C[5] = S[3 xor 2] = S[1] |
102 RATE TEAR ETAEAA 32 ABCDEFGHIJKLMNOPQRSTUVWXYZ._!?,; ;ABCDEFGHIJKLMNOPQRSTUVWXYZ._!?, MOMCUKZ,ZPD 1956 ACEHINT_ ACTN_IHE CIANCTNAAIECIA_TAI 0Sample Output
TEETER HELLO_WORLD THE_CAT_IN_THE_HAT
题目分析:
此题题目意思很难理解但理解之后就很简单就能过掉!
加密算法计算C为如下:
1.计算余数的整数d由n个分割后的整数部分。这可以更简洁地表示为d=(int)(N的1.5次方+ x)%n,其中“%”是求余运算符。2.集合C[d]在S中的符号的位置是相同的位置的M[d]在P。3.对于每个j!= D;0.. n-1个,设置C [J]符号在S的位置异或上M[(j +1)%N]与 S相同的位置.注意按位异或运算符“^”在C,C+ +和Java。
例如;
x=102, S=RATE, P=TEAR, C=ETAEAA, n=strlen(C)=6;
1.d=pow(6*1.0,1.5)+x)%n; d=2;
2.因为C[2]=S[1];所以 M[2]=P[1]
3.现在求M[1]
因为C[1]=S[2];
M[(1+1)%6]=M[2]=S[3]
所以 M[1]=P[2^3];
依次类推求出M;
代码:
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <vector>#include <queue>#include <cstdio>#include <cmath>#include <string>#include <stack>using namespace std;int main(){int n,x,i,j,k,l1,l2,l3,d,a;char s[40],p[40],m[70],c[70];while(~scanf("%d",&x)&&x){cin>>s>>p>>c;l1=strlen(s);l3=strlen(c);d=((int)pow(l3*1.0,1.5)+x)%l3;//求出dmemset(m,0,sizeof(m));for(i=0; i<l1; i++)if(c[d]==s[i])//求出C[d];break;m[d]=p[i];for(i=d-1; (i+l3)%l3!=d; i--){if(i<0)i=(i+l3)%l3;for(j=0; j<l1; j++)//找出c[i]在S中相等的位置j;if(c[i]==s[j])break;for(k=0; k<l1; k++)//找出m[(i+1)%l3]在S中相等的位置k;if(m[(i+1)%l3]==s[k])break;a=j^k;m[i]=p[a];//求出m[i];}printf("%s\n",m);}return 0;}
相关文章推荐
- POJ 3258 River Hopscotch [NOIP2015 D2T1] (洛谷 P2678 跳石头)
- poj2015--IP Address
- POJ 2015 Permutation Code
- POJ 2015 Permutation Code
- POJ_3258_River_Hopscotch_[NOIP2015]_(二分,最大化最小值)
- poj2015-Permutation Code
- Poj(2407),Greater New York Regional 2015 (D)
- POJ 2015 JAVA
- poj 2015 Permutation Code
- POJ - 3258/USACO - Dec06 Silver/NOIP 2015 - 提高组 River Hopscotch 贪心+二分搜索
- poj 2015 IP Address
- POJ 2015 IP Address
- POJ 3258 (NOIP2015 D2T1跳石头)
- POJ 3258 (NOIP2015 D2T1跳石头)
- NOIP 2015 d2t1【poj 3258】 River Hopscotch 二分
- |poj 1523|割顶|[HAOI2015]SPF
- poj 2015 Permutation Code
- Poj.3233 Matrix Power Series【矩阵快速幂】 2015/07/29
- POJ - 2955 Brackets 区间DP
- POJ2559 - Largest Rectangle in a Histogram - 单调栈+DP