POJ 2015 JAVA

来源:互联网 发布:淘宝首页全屏代码 编辑:程序博客网 时间:2024/06/03 18:28

http://poj.org/problem?id=2015

题目太长了,意思简单地描述一下
就是说给出密钥:码串 S 和变换码串P还有一个整数x,可以根据明文 M(长度为 n),求出暗文C。
首先,S的长度是2的正整数次幂的字母集合
顾名思义,P就是S中所有字母的另一种排列方式,而M和C的长度相同,且字符都来源于S。


加密方式:
1. d =( n^1.5 + x )% n
2. 若 M[ d ]=P[ R ],则 C[ d ] =S[ R ]
3. 对于C中其他位置的字符,设所在位置为 j ( j = 0..n-1,j≠d ),设 M[j] 在 P 中的位置为 r1,M[(j+1) % n] 在 S 中的位置为 r2
则 C[ j ] = S[ r1^r2 ]

最后需要一个解密代码


输入方式
x
S
P
C
...
结束输入为x=0
思路为从d开始往前递推,原来想用dp的,因为没考虑到d的作用。


一次AC,下面是代码


import java.util.Scanner;



public class Permutation_Code {
static int x,d,n,l;
static char[] s,p,c,m;
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String S,P,C,M;
while(0!=(x=Integer.parseInt(sc.nextLine()))){
S=sc.nextLine();
P=sc.nextLine();
C=sc.nextLine();
l=S.length();
n=C.length();

m=new char[n];
s=S.toCharArray();
p=P.toCharArray();
c=C.toCharArray();
d=((int)(Math.pow(n, 1.5)+x))%n;
m[d]=p[R(s,c[d])];
int count=1,j=d;
while(count<n){
int J=(j--+n)%n;
int Jm=(J-1+n)%n;
int r2=R(s,m[J]);
int r=R(s,c[Jm]);
m[Jm]=p[r^r2];
count++;
}
System.out.println(m);
}
}


private static int R(char[] r,char c){
for(int i=0;i<r.length;i++){
if(r[i]==c)
return i;
}
return -1;
}
}
0 0