HDU1048——史上最难的题

来源:互联网 发布:杉帝网络 编辑:程序博客网 时间:2024/05/02 04:59

Problem Description
Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line - A single line, “START”

Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line - A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

Output
For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

AC代码:#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int main(){    char a[27]="VWXYZABCDEFGHIJKLMNOPQRSTU";//这是明文文本,题目中给出的文本向左移5位即为答案,需要得到的是加密的文本。     char str[1000];    int i,len;    while(cin.getline(str,1000))//getline遇回车或者输入字符数上限结束,此处用于回车。     {        if(strcmp(str,"ENDOFINPUT")==0) //结束输入           break;        if(strcmp(str,"START")!=0&&strcmp(str,"END")!=0) //如果不为START或者END就进行文本的输出。         {        len=strlen(str);        for(i=0;i<len;i++)        {            if(str[i]>='A'&&str[i]<='Z')               printf("%c",a[str[i]-'A']);//很巧妙此处!             else               printf("%c",str[i]);        }         cout<<endl;           }        }    return 0;    }

优化代码2:

解题思路:这是一道使用map最经典的例子!ZOJ 1392AC代码:#include<iostream>#include<string>#include<map>using namespace std;int main(){    string s;    map<char,char> M;    M['A']='V';    M['B']='W';    M['C']='X';    M['D']='Y';    M['E']='Z';    M['F']='A';    M['G']='B';    M['H']='C';    M['I']='D';    M['J']='E';    M['K']='F';    M['L']='G';    M['M']='H';    M['N']='I';    M['O']='J';    M['P']='K';    M['Q']='L';    M['R']='M';    M['S']='N';    M['T']='O';    M['U']='P';    M['V']='Q';    M['W']='R';    M['X']='S';    M['Y']='T';    M['Z']='U';    char ss[200];    int i;    while(cin.getline(ss,200)){        s=ss;        if(s.compare("START")==0)           continue;        else if(s.compare("END")==0)            continue;            else if(s.compare("ENDOFINPUT")==0)             break;          else           {           for(i=0;i<s.size();i++){            if(s[i]>='A'&&s[i]<='Z')               cout<<M[s[i]];            else                 cout<<s[i];           }                cout<<endl;          }    }    return 0;}
0 0
原创粉丝点击