zoey
点击国内主页可以让浏览速度加快哦 ~ !

关于HDOJ 1048题(The Hardest Problem Ever) 的理解(C/C++)

2019-08-09 hdoj 加密 字符串 gets函数 getline函数
Word count: 779 | Reading time: 4min

题目描述

                       The Hardest Problem Ever
  Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
  Total Submission(s): 34272 Accepted Submission(s): 15887

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

Source
  South Central USA 2002

原题链接

More info:Question

getline()函数

  getline的函数原型是:

istream& getline ( istream &is , string &str , char delim );

istream& getline ( istream& , string& );

功能:将输入流is中读到的字符存入str中(直到遇到终结符delim才结束)。

gets()函数

  gets的函数原型是:

char  *   gets(char *   buffer);

功能:从std in流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。

返回值:读入成功,返回与参数buffer相同的指针;读入过程中遇到EOF(End-of-File)或发生错误,返回NULL指针。

Accepted代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string>
using namespace std;
string a;
string alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void fun(string& x) {
for (int i = 0; i < x.size(); i++) {
if (isalpha(x[i])) {
int k = (x[i] - 'A' + 26 - 5) % 26;
x[i] = alp[k];
}
}
}
int main() {
while (getline(cin, a, '\n')) {
if (a == "ENDOFINPUT") break;

if(a == "START")
while (1) {
getline(cin,a);
if (a == "END") break;
fun(a);
cout << a << endl;
}

}
return 0;
}

参考博客

非常感谢peaceful-andy的博文

Author: Zoey

Link: https://zoey1038569979.github.io/2019/08/09/hdoj1048/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
关于HDOJ 1058题(Humble Numbers(有且仅有质因数2,3,5,7组成的数)) 的理解(C/C++)
NextPost >
关于HDOJ 1028题(Ignatius and the Princess III(n=哪些正整数相加的可能)) 的理解(C/C++)
CATALOG
  1. 1. 题目描述
  2. 2. 原题链接
  3. 3. getline()函数
  4. 4. gets()函数
  5. 5. Accepted代码
  6. 6. 参考博客