天天看點

Substitution Cypher/替換加密字元串 - ACM

Substitution Cypher

Time Limit: 1.0 Seconds   Memory Limit: 65536K    Multiple test files

    Substitution cyphers are the simplest of cyphers where the letters of one

alphabet are substituted for the letters of another alphabet. In one form or

another, they've been in use for over 2000 years.

Input

    A line containing the plaintext alphabet

    A line containing the substitution alphabet

    Several lines of text

Output

    A line containing the substitution alphabet

    A line containing the plaintext alphabet

    The converted lines of text

Please note:

    All lines will be at most 64 characters, plus a trailing end-of-line

character. Pass through all characters not found in the plaintext alphabet.

Sample Input

abcdefghijklmnopqrstuvwxyz

zyxwvutsrqponmlkjihgfedcba

Shar's Birthday:

The birthday is October 6th, but the party will be Saturday,

October 5.  It's my 24th birthday and the first one in some

years for which I've been employed.  Plus, I have new clothes.

So I have cause to celebrate.  More importantly, though,

we've cleaned the house!  The address is 506-D Albert Street.

Extra enticement for CS geeks:  there are several systems in

the house, and the party is conveniently scheduled for 3 hours

after the second CSC programming contest ends (not to mention,

within easy walking distance)!

Sample Output

zyxwvutsrqponmlkjihgfedcba

abcdefghijklmnopqrstuvwxyz

Sszi'h Brigswzb:

Tsv yrigswzb rh Oxglyvi 6gs, yfg gsv kzigb droo yv Szgfiwzb,

Oxglyvi 5.  Ig'h nb 24gs yrigswzb zmw gsv urihg lmv rm hlnv

bvzih uli dsrxs I'ev yvvm vnkolbvw.  Pofh, I szev mvd xolgsvh.

Sl I szev xzfhv gl xvovyizgv.  Mliv rnkligzmgob, gslfts,

dv'ev xovzmvw gsv slfhv!  Tsv zwwivhh rh 506-D Aoyvig Sgivvg.

Ecgiz vmgrxvnvmg uli CS tvvph:  gsviv ziv hvevizo hbhgvnh rm

gsv slfhv, zmw gsv kzigb rh xlmevmrvmgob hxsvwfovw uli 3 slfih

zugvi gsv hvxlmw CSC kiltiznnrmt xlmgvhg vmwh (mlg gl nvmgrlm,

drgsrm vzhb dzoprmt wrhgzmxv)!

題目意思:

    第一行:密文字元串

    第二行:待加密的字元串

  第三行:<==第二行>

  第四行:<==第一行>

    後續行:待加密的文本

    每行最多64個字元(加上回車)

    第二行按理說應該和第一行長度相等,一一對應

解題方法:

    在<待加密的文本>中, 如果在<待加密的字元串>數組中找到了該字元,則

用<待加密的字元串>對應的<密文字元串>替換即可.

#include <stdio.h>
#include <string.h>

int main(void)
{
    char a[64];
    char b[64];
    char s[64];

    int i,j,len,len_a;

    gets(a);
    gets(b);
    puts(b);
    puts(a);

    len_a = strlen(a);

    while(gets(s)){
        len = strlen(s);
        for(i=0; i<len; i++){
            for(j=0; j<len_a; j++){
                if(s[i] == a[j]){
                    printf("%c", b[j]);
                    break;
                }
            }
            if(j == len_a){
                printf("%c",s[i]);
            }
        }
        printf("\n");
    }
    return 0;
}      

測試截圖:

Substitution Cypher/替換加密字元串 - ACM

女孩不哭 @ 2013-05-27 00:47:49 @ http://www.cnblogs.com/nbsofer

繼續閱讀