SRM 147 DIV2 Level One - シーザー暗号のデコード
シーザー暗号のデコード。
エンコード(プラス側にシフト)は簡単なんだけど、デコードはマイナスが入るのでちとメンドイ。
とりあえずエンコードの時と全く同じような形を作り出すことにした。
#include <string> using namespace std; #define foreach(type ,bind, item) \ for (type::iterator bind = (item).begin(), __ie__ = (item).end() ;\ bind != __ie__ ; \ bind++) class CCipher { public: static string decode(string cipher, int shift) { foreach (string, i, cipher) *i = (*i - 'A' + 26 - shift % 26) % 26 + 'A'; return cipher; } };
26 - shift % 26として値が必ずプラスになるように保つ。
入力文字が長いなら、表を作ってからデコードしたほうが早そう。
decode!はあんまり気持ちよくないな。