题目要求是将密文逆回去得到明文。
首先密文的矩阵的行数是strlen(str) / key,其中str表示输入的密文,如果不整除,需要加1。
然后就是把字符填回矩阵,注意到可能会出现矩阵某个位置是空的,而且空的那一个行从那个位置开始往后都是空的。
于是需要控制一个L,表示从那个位置开始后的列都是L。
然后L需要在rest%(k-st-1) == 0 && rest/(k-st-1) < L这个条件时减1。
这个表示剩余的字符数rest能被剩余的列数整除,而且除出来的结构小于L,这个稍微画一下就能理解出来。
考虑到矩阵的列数不确定,所以采用了vector容器。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <string> #define LL long long using namespace std; char str[100005]; int k, len; vector <char> m[100005]; void Work() { gets(str); scanf("%d", &k); getchar(); int now = 0, L, rest = strlen(str); len = rest/k + 1; if (rest%k == 0) len--; for (int i = 0; i < len; ++i) m[i].clear(); L = len; for (int st = 0; st < k; ++st) { for (int i = 0; i < L; ++i) { if (str[now] == '