TopCoder算法竞赛题3:SRM 147 DIV 2, 250-point
2010-01-26 23:45
666 查看
Problem Statement
Julius Caesar used a system of cryptography, now known as Caesar Cipher, which shifted each letter 2 places further through the alphabet (e.g. 'A' shifts to 'C', 'R' shifts to 'T', etc.). At the end of the alphabet we wrap around, that is 'Y' shifts to 'A'.
We can, of course, try shifting by any number. Given an encoded text and a number of places to shift, decode it.
For example, "TOPCODER" shifted by 2 places will be encoded as "VQREQFGT". In other words, if given (quotes for clarity) "VQREQFGT" and 2 as input, you will return "TOPCODER". See example 0 below.
Definition
Class: CCipher
Method: decode
Parameters: string, int
Returns: string
Method signature: string decode(string cipherText, int shift)
(be sure your method is public)
Constraints
cipherText has between 0 to 50 characters inclusive
each character of cipherText is an uppercase letter 'A'-'Z'
shift is between 0 and 25 inclusive
Examples
0)
"VQREQFGT"
2
Returns: "TOPCODER"
1)
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10
Returns: "QRSTUVWXYZABCDEFGHIJKLMNOP"
2)
"TOPCODER"
0
Returns: "TOPCODER"
3)
"ZWBGLZ"
25
Returns: "AXCHMA"
4)
"DBNPCBQ"
1
Returns: "CAMOBAP"
5)
"LIPPSASVPH"
4
Returns: "HELLOWORLD"
Source code
#include <iostream>
#include <iterator>
using namespace std;
class CCipher
{
public:
string decode(string cipherText, int shift)
{
string s = "";
for(int i=0; i<(int)cipherText.size(); i++)
{
char c = cipherText[i];
c = ((c-'A')-shift+26)%26 + 'A';
s += c;
}
return s;
}
};
int main()
{
ostream_iterator<char> output(cout, "");
CCipher cipher;
string str = "VQREQFGT";
string Str = cipher.decode(str,2);
copy(Str.begin(), Str.end(), output);
cout<<endl;
return 0;
}
Julius Caesar used a system of cryptography, now known as Caesar Cipher, which shifted each letter 2 places further through the alphabet (e.g. 'A' shifts to 'C', 'R' shifts to 'T', etc.). At the end of the alphabet we wrap around, that is 'Y' shifts to 'A'.
We can, of course, try shifting by any number. Given an encoded text and a number of places to shift, decode it.
For example, "TOPCODER" shifted by 2 places will be encoded as "VQREQFGT". In other words, if given (quotes for clarity) "VQREQFGT" and 2 as input, you will return "TOPCODER". See example 0 below.
Definition
Class: CCipher
Method: decode
Parameters: string, int
Returns: string
Method signature: string decode(string cipherText, int shift)
(be sure your method is public)
Constraints
cipherText has between 0 to 50 characters inclusive
each character of cipherText is an uppercase letter 'A'-'Z'
shift is between 0 and 25 inclusive
Examples
0)
"VQREQFGT"
2
Returns: "TOPCODER"
1)
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10
Returns: "QRSTUVWXYZABCDEFGHIJKLMNOP"
2)
"TOPCODER"
0
Returns: "TOPCODER"
3)
"ZWBGLZ"
25
Returns: "AXCHMA"
4)
"DBNPCBQ"
1
Returns: "CAMOBAP"
5)
"LIPPSASVPH"
4
Returns: "HELLOWORLD"
Source code
#include <iostream>
#include <iterator>
using namespace std;
class CCipher
{
public:
string decode(string cipherText, int shift)
{
string s = "";
for(int i=0; i<(int)cipherText.size(); i++)
{
char c = cipherText[i];
c = ((c-'A')-shift+26)%26 + 'A';
s += c;
}
return s;
}
};
int main()
{
ostream_iterator<char> output(cout, "");
CCipher cipher;
string str = "VQREQFGT";
string Str = cipher.decode(str,2);
copy(Str.begin(), Str.end(), output);
cout<<endl;
return 0;
}
相关文章推荐
- TopCoder算法竞赛题2:SRM 146 DIV 2, 250-point
- TopCoder算法竞赛题5:SRM 149 DIV 2, 250-point
- TopCoder算法竞赛题4:SRM 148 DIV 2, 250-point
- TopCoder算法竞赛题1:SRM 144 DIV 2, 250-point
- TopCoder算法竞赛题6:SRM 150 DIV 2, 250-point
- 小朋友学TopCoder(7):SRM726 DIV2 250-point
- SRM 667 DIV 2 PointDistance 250-point
- SRM 669 DIV 2 LiveConcert 250-point
- TopCoder 250 points 8-SRM 147 DIV 2 176.36/250 70.54%
- SRM 670 DIV 2 Cdgame 250-point
- 【topcoder SRM 652 DIV2 250】ValueOfString
- SRM 145 DIV 2 250
- SRM147_DIV2
- TopCoder_SRM_144_DIV2_250_Time
- TopCoder SRM 667 Div1 Problem 250 - OrderOfOperations (状压dp)
- topcoder SRM 541 div2 250
- topcoder srm 610 div2 250
- SRM 594 DIV1 250
- TopCoder 250 points 15-SRM 151 DIV 1 121.73/250 48.69%
- TopCoder 250 points 18-SRM 152 DIV 2 167.05/250 66.82%