首页 > 试题广场 >

playfair

[编程题]playfair
  • 热度指数:172 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
用playfair加密信息。加密过程中的j都由i来代替playfair加密算法首先需要绘制密码表,密码表是一个5*5的矩阵,开始由密钥按顺序排列,其余按照未出现的字母顺序。若密钥中含有重复字母需要将重复字母去掉,若有j用i来代替,例如密钥为nowcoder,得到的密码表为

加密明文需要符合以下规则:
  1. 将明文中每两个字母组成一对,若成对后是两个相同字母或留下一个字母无法成对,则不进行变化直接放入密文中
  2. 明文对p1p2在同一行,对应密文对c1c2分别为紧靠p1p2右端的字母,最后一列右端对应第一列。例如明文对为rf,对应密文对为ae
  3. 明文对p1p2在同一列,对应密文对c1c2分别为紧靠p1p2下方的字母,最后一行下方对应第一行。例如明文对为rv,对应密文对为ho
  4. 明文对p1p2不在同一行且不在同一列,对应密文对c1c2分别为p1p2确定的矩形中的同行另外两角,例如明文对为hb,对应密文对为kr
求密文是什么吗?

示例1

输入

"nowcoder","iloveyou"

输出

"kgrobunv"

备注:
,输入保证都为小写字母
class Solution:
    def Encode(self , key , str ):
        # write code here
        uniquekey =[]
        for x in key:
            if x =='j':
                x = 'i'                
            if x not in uniquekey:
                uniquekey.append(x)
        table =['']*26
        for i in range(0,26):
            table[i] = chr(ord('a')+ i)
        table.remove('j')
        for x in uniquekey:
            table.remove(x)
        for x in table:
            uniquekey.append(x)
        playfair = [['']*5]*5
        k=0
        while k<25:
            for i in range(0,5):
                tmp=[]
                for j in range(0,5):
                    tmp.append(uniquekey[k])
                    k+=1
                playfair[i]=tmp
        s11=''
        s2=''
        strlen = len(str)
        if strlen%2!=0:
            s11=str[0:-1]
            s2=str[-1]
        else:
            s11=str
        s1=''
        for x in s11:
            if x!='j':
                s1+=x
            else:
                s1+='i'
        tmp=0
        res=''
        while tmp<len(s1)-1:
            if s1[tmp]==s1[tmp+1]:
                res+=s1[tmp]
                res+=s1[tmp+1]
                tmp+=2
                continue
            x1,x2,y1,y2=0,0,0,0
            for i in range(0,5):
                for j in range(0,5):
                    if playfair[i][j]==s1[tmp]:
                        x1=i
                        y1=j
                    elif playfair[i][j]==s1[tmp+1]:
                        x2=i
                        y2=j
            if x1==x2:
                if y1==4:
                    y1=-1
                if y2==4:
                    y2=-1
                res+=playfair[x1][y1+1]
                res+=playfair[x1][y2+1]
                tmp+=2
            elif y1==y2:
                if x1==4:
                    x1=-1
                if x2==4:
                    x2=-1
                res+=playfair[x1+1][y1]
                res+=playfair[x2+1][y1]
                tmp+=2
            else:
                res+=playfair[x1][y2]
                res+=playfair[x2][y1]
                tmp+=2
        res+=s2
        return res
编辑于 2021-03-28 22:14:49 回复(0)
 public String Encode (String key, String str) {
        str=str.replace("j","i");
        key=key.replace("j","i");
        Map<Character,Integer> map=new HashMap<>();
        List<Character> list=new ArrayList<>();
        int index=0;
        for(int i=0;i<key.length();i++) {
        	char c=key.charAt(i);
        	if(!map.containsKey(c)) {
        		map.put(c,index++);
        		list.add(c);
        	}
        }
        for(int i=0;i<26;i++) {
        	char c=(char)('a'+i);
        	if(c=='j')
                continue;
        	if(!map.containsKey(c)) {
        		map.put(c,index++);
        		list.add(c);
        	}
        }
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<str.length();i+=2) {
        	char c1=str.charAt(i);
        	if(i==str.length()-1) {
        		sb.append(c1);
        		break;
        	}
        	char c2=str.charAt(i+1);
        	if(c1==c2) {
        		sb.append(c1).append(c1);
        	}else {
        		getFair(map,list,c1,c2,sb);
        	}
        }
		return sb.toString();
    }
    private  void getFair(Map<Character,Integer> map, List<Character> list,char c1,char c2,StringBuilder sb) {
		int n1=map.get(c1);
		int n2=map.get(c2);
		int x1=n1/5;
		int y1=n1%5;
		int x2=n2/5;
		int y2=n2%5;
		
		if(x1==x2) {
			y1=(++y1==5?0:y1);
			y2=(++y2==5?0:y2);
		}else if(y1==y2) {
			x1=(++x1==5?0:x1);
			x2=(++x2==5?0:x2);
		}else {
			int tmp=y1;
			y1=y2;
			y2=tmp;
		}
		n1=x1*5+y1;
		n2=x2*5+y2;
		sb.append(list.get(n1)).append(list.get(n2));
	}



发表于 2021-03-11 20:54:28 回复(0)

问题信息

难度:
2条回答 2271浏览

热门推荐

通过挑战的用户

查看代码