poj2015-Permutation Code
Description
As 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. You will 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 some but 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:
For example, consider this scenario where S=RATE, P=TEAR, x=102, M=TEETER, and n=6. To compute d, first calculate 6
1.5 + 102 = 116.696938, then take the remainder after dividing by 6. So d = 116 % 6 = 2. The following table shows the steps in filling in the cyphertext C. Note that the order of the steps is not important.
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]
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 on your knowledge of the encoding algorithm.
Input
The 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 string P, 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 most sixty 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.
Output
For each input set print the decrypted string on a single line, as shown in the sample output.
Sample Input
102
RATE
TEAR
ETAEAA
32
ABCDEFGHIJKLMNOPQRSTUVWXYZ._!?,;
;ABCDEFGHIJKLMNOPQRSTUVWXYZ._!?,
MOMCUKZ,ZPD
1956
ACEHINT_
ACTN_IHE
CIANCTNAAIECIA_TAI
0
Sample Output
TEETER
HELLO_WORLD
THE_CAT_IN_THE_HAT
문제 해결 방법:
이 문제를 자세히 분석하면 이것은 매우 물 같은 시뮬레이션 문제이며 관건은 이 문제의 돌파구를 찾는 것이다.분석을 통해 m[d]가 돌파구라는 것을 알 수 있고 다른 m[i]는 한 번에 추측할 수 있다.
수학 공식 주의:if a^b=c---->a=b^c
내 AC 코드는 다음과 같습니다.
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int x,i,j;
while(~scanf("%d",&x))
{
char s[35],p[35],c[70],m[70];
int splen,n,d;
if(x==0) break;
scanf("%s
%s
%s",s,p,c);
splen=strlen(s);
n=strlen(c);
d=((int)(pow(n,1.5))+x)%n;
for(i=0;i<splen;i++)
if(c[d]==s[i])
m[d]=p[i];
int temp1,temp2;// c[i] s , m[(i+1)%n] s
for(i=d-1;(i+n)%n!=d;i--)
{
if(i<0)
i=(i+n)%n;
for(j=0;j<splen;j++)
if(c[i]==s[j])
{
temp1=j;
break;
}
for(j=0;j<splen;j++)
if(m[(i+1)%n]==s[j])
{
temp2=j;
break;
}
m[i]=p[temp1^temp2];//
}
for(j=0;j<n;j++)
printf("%c",m[j]);
printf("
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.