⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 squarecypher.cpp

📁 矩阵加密算法:SquareCypher. c++实现 vs.net2005
💻 CPP
字号:
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <sstream>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <cmath>
#include <complex>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define SIZE(t) ((int)((t).size()))
struct nd{
	char ch;
	int pos;
};
struct comp{
	bool operator() (nd aa, nd bb)
	{ return aa.pos < bb.pos; }   
};

class SquareCypher
{
        public:
        string decrypt(string c)
        {
            int n = c.size();
			int m = (int)sqrt(float(n-1)) - 1;
			if(m == 0)
				return c;
			vector<nd> v;
			v.resize(n);
			int cur = 2;
			for(int i = 0; i < n; i++)
			{
				if(i < m+2)
				{
					v[i].ch = c[i];
					v[i].pos = i*i;
				}
				else
				{
					v[i].ch = c[i];
					int k = (int)sqrt(float(cur));
					if(k *k == cur)
						cur++;
					v[i].pos = cur;
					cur++;
				}
			}
			sort(v.begin(), v.end(), comp());
			string res;
			for(int i = 0; i < n; i++)
				res += v[i].ch;
			return res;
        }
        
// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { string Arg0 = "thinissacotest"; string Arg1 = "thisisacontest"; verify_case(0, Arg1, decrypt(Arg0)); }
	void test_case_1() { string Arg0 = "thisisacontest"; string Arg1 = "thisiaconstest"; verify_case(1, Arg1, decrypt(Arg0)); }
	void test_case_2() { string Arg0 = "heoll"; string Arg1 = "hello"; verify_case(2, Arg1, decrypt(Arg0)); }
	void test_case_3() { string Arg0 = "test"; string Arg1 = "test"; verify_case(3, Arg1, decrypt(Arg0)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main()
{
        SquareCypher ___test;
        ___test.run_test(-1);
        return 0;
}
// END CUT HERE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -