zoukankan      html  css  js  c++  java
  • CDOJ 1270 Playfair(模拟)

    题目链接

    Playfair is a kind of substitution cipher.And the encryption role is simple.In general,there are three steps in Playfair algorithm.

    Step 1: Creat a 5*5 secret key table.

    Step 2:Tidy plaintext : only leave letters and omit others And if there are capital letters in the plaintext,change it into lower letters.

    Step 3: Work out the ciphertext.

    Now, let’s see an example.

    Firstly,we should creat a 5*5 secret key table.Let’s assume that “ulysses” is the key.If there is a letter appearing more than once we should only retain the first one and delete the others.So we can get the real key “ulyse”.Then we will fill the secret key table with the key “ulyse” and with the other letters which haven’t appeared in the key “ulyse” in order(assume j is equal to i,so every element in the table is unique and j won’t be in this table).Finally, we will get a secret key table as follows:

    title

    Secondly,assuming that we will send a important message “balloon”.We should divide ballon into a few groups with exactly two letters.But if the two letters are the same,a single x will be inserted between the same two letters.So “balloon” will be divided into “ba” “lx” “lo” “on”.

    Finally,we will encrypt the message.The substitution rules are followed:

    1.If the group’s two letters are in the same row in the key table,each letter will be changed into the letter which is just on the right of it.(the first column is seen as on the right of the fifth column).

    2.If the group’s two letters are in the same column in the key table,each letter will be changed into the letter which is adjacently below it.(the first row is seen as adjacent below fifth column).

    3.If the group’s two letters are neither in the same row nor in the same column in the key table,each letter will be changed into the letter whose position is the same row as itself and the same column as another letter.

    So “balloon” will be changed into “cbsvbvpo”.

    Ps: j in the plaintext is equal to i and all the letter both in plaintext and ciphertext should be lower letter.And if there are capital letters in the plaintext,change it into lower letters.

    Input

    The first line will contain the secret key(no more than 25 letters). Then you will get a article(including capital letter and the lower letter).You should omit all the spacing , the punctuation and the numbers(maybe has two or more rows).If there are odd letters after tidying plaintext,delete the last letter.The article would end with a ‘*’.

    Ps:You can assume that the article is no more than 10000 letters.And “xx” won’t exist. And the secret key only contains lower letters.

    Output

    The ciphertext which only includes lower letters.

    Sample input and output

    Sample InputSample Output
    cipher
    balloon
    *
    dbspgsug
    cipher
    fill book*
    aespslsvqg
    

    Source

    第七届ACM趣味程序设计竞赛第四场(正式赛)
     
    题解:(见百度百科==)给出一个key,是一个小于25个字母的字符串,然后将这个字符串中的重复字符去掉,横向填入一个5*5的格子里,格子中没填满的用26个字母中没用过的按字典序填入。由于字母有26个,格子是25个,所以字母i和j当做同一个字符处理,均用i表示。然后给出一段包含各种字符的字符串,需要提取其中的字母,字符串的结尾是‘*’。提取出的字母按顺序两两配对,如果两个字母相同,则在中间插入一个‘x’,前一个字母与‘x'配对,后一个字母与该字母的后一字母配对,如果仍相同,也这样处理(不会出现’xx‘这样的数据)。配对后,每两个字母到密码表中匹配,如果同行,则输出相应字母的右边的字母;如果同列,则输出相应字母的下面的字母(第一行为第五行下面,第一列为第五列右边);如果不同行部同列,输出两个字母构成的矩形的另外两个角上的字母(其实就是交换两者的Y坐标并输出)。
    自己写的代码一直WA...==b,还没找出错误(结尾给出我的WA代码)...感谢YXT提供的AC代码.
     
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    int len1,i,j,k,x1,x2,y1,y2;
    int f[10005],mp[6][6],a[10005];
    char ch1[10005];
    
    int xt(char s)
    {
        if ((int)s>=97) return (int)s-96;
        return (int)s-64;
    }
    
    void solve()
    {
        int i,j,f[30];
        len1=strlen(ch1);
        j=0;
        i=1;
        memset(f,0,sizeof(f));
        for(int ii=0; ii<len1; ii++)
        {
            int k=xt(ch1[ii]);
            if (!f[k])
            {
                if (k==10 || k==9)
                    if (f[9]) continue;
                    else k=9;
                j++;
                f[k]=1;
                mp[i][j]=k;
                if (j==5)
                {
                    j=0;
                    i++;
                }
            }
        }
        for(int ii=1; ii<=26; ii++)
            if (!f[ii])
            {
                if (ii==10) continue;
                j++;
                mp[i][j]=ii;
                if (j==5)
                {
                    j=0;
                    i++;
                }
            }
        return;
    }
    
    void findxy(int *x,int *y,int z)
    {
        int i,j;
        for(i=1; i<=5; i++)
            for(j=1; j<=5; j++)
                if (mp[i][j]!=z) continue;
                else
                {
                    *x=i;
                    *y=j;
                    return;
                }
    }
    
    int main()
    {
        freopen("in.txt", "r", stdin);
        while(~scanf("%s",&ch1))
        {
            solve();
    
            memset(a,0,sizeof(a));
            while (~scanf("%s",&ch1))
            {
                len1=strlen(ch1);
                for(i=0; i<len1; i++)
                {
                    if (ch1[i]=='*') goto mm;
                    if(!(ch1[i]>='a' && ch1[i]<='z' || ch1[i]>='A' && ch1[i]<='Z')) continue;
                    if (ch1[i]=='j' || ch1[i]=='J') ch1[i]='i';
                    a[++a[0]]=xt(ch1[i]);
                }
            }
    mm:
            k=1;
            while(k<a[0])
            {
                findxy(&x1,&y1,a[k]);
                if (a[k+1]==a[k]) findxy(&x2,&y2,24);
                else
                {
                    k++;
                    findxy(&x2,&y2,a[k]);
                }
    
                if (x1==x2 && y1!=y2) printf("%c%c",(char)(96+mp[x1][y1%5+1]),(char)(96+mp[x2][y2%5+1]) );
                if (x1!=x2 && y1==y2) printf("%c%c",(char)(96+mp[x1%5+1][y1]),(char)(96+mp[x2%5+1][y2]) );
                if (x1!=x2 && y1!=y2) printf("%c%c",(char)(96+mp[x1][y2]),(char)(96+mp[x2][y1]) );
                k++;
            }
            printf("
    ");
        }
        return 0;
    }
    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #define PI 3.1415926
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    using namespace std;
    #define LOCAL
    char mp[10][10];
    int cha[30];
    string key,line,art;
    struct Node
    {
        char a,b;
    };
    void mpfill(int idx,char c)//填密码表
    {
        int i=idx/5,j=idx%5;
        mp[i][j]=c;
        return;
    }
    void mpmake()//生成密码表
    {
        ms(cha),msp,cha[9]=1;
        int idx=0;
        string::iterator it;
        for(it=key.begin(); it!=key.end(); it++)
        {
            if(isalpha(*it)==0)continue;
            *it=tolower(*it);
            if(cha[*it-'a']==0)
            {
                mpfill(idx++,*it);
                cha[*it-'a']=1;
            }
        }
        for(int i=0; i<26; i++)
        {
            if(cha[i]==0)
            {
                mpfill(idx++,i+'a');
                cha[i]=1;
            }
        }
    }
    void scanart()
    {
        line.clear(),art.clear();
        while(cin>>line)
        {
            string::iterator it;
            for(it=line.begin(); it!=line.end(); it++)
            {
                if(*it=='*')return;
                if(isalpha(*it))
                {
                    if(tolower(*it)=='j') art.push_back('i');
                    else art.push_back(tolower(*it));
                }
            }
        }
    }
    void mpfind(int i1,int j1,int i2,int j2)
    {
        if(i1==i2)//同行
        {
            j1=(j1+1)%5,j2=(j2+1)%5;
            printf("%c%c",mp[i1][j1],mp[i2][j2]);
            return;
        }
        if(j1==j2)//同列
        {
            i1=(i1+1)%5,i2=(i2+1)%5;
            printf("%c%c",mp[i1][j1],mp[i2][j2]);
            return;
        }
        //不同行不同列
        printf("%c%c",mp[i1][j2],mp[i2][j1]);
    }
    void scanmp(char c1,char c2)
    {
       int i1,i2,j1,j2;
        for(int i=0;i<25;i++)
        {
            int x=i/5,y=i%5;
            if(mp[x][y]==c1)i1=x,j1=y;
            if(mp[x][y]==c2)i2=x,j2=y;
        }
        mpfind(i1,j1,i2,j2);
    }
    void change()
    {
        string::iterator it;
        for(it=art.begin(); it!=art.end(); it++)
        {
            Node t;
            if(it+1==art.end())break;
            if(*it!=*(it+1))t.a=*it,t.b=*(it+1),it++;
            else t.a=*it,t.b='x';
            scanmp(t.a,t.b);
        }
        printf("
    ");
        return;
    }
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
    #endif // LOCAL
        ios::sync_with_stdio(false);
        while(cin>>key)
        {
            mpmake();//密码表
            scanart();//读取明文
            change();//转换密文
        }
        return 0;
    }
  • 相关阅读:
    C++数据类型之实型(浮点型)&科学计数法
    C++之数据类型--整形&sizeof关键字
    C++之关键字&标识符命名规则
    C++之常量
    C++之变量
    zabbix3.x添加华为(93069306)网络设备详解
    一些最常见的SNMP的OID自动翻译成zabbix数字进行表示(华为9306)
    Linux:日期用法,及格式定义
    linux awk命令详解
    Bash常用快捷键及其作用
  • 原文地址:https://www.cnblogs.com/gpsx/p/5181605.html
Copyright © 2011-2022 走看看