假设有一种加密字符串的方法:
例如,单词FRUIT按以下方式加密:
We append the character $ at the end of the word:
FRUIT$
We then form all the strings by moving the first character at the end:
FRUIT$
RUIT$S
UIT$FR
IT$FRU
T$FRUI
$FRUIT
Then we sort the new strings into alphabetical order:
$FRUIT
FRUIT$
IT$FRU
RUIT$F
T$FRUI
UIT$FR
The encrypted string:
T$UFIR
Run Code Online (Sandbox Code Playgroud)
现在我的问题很明显:如何将给定的字符串解密为原始形式.
我现在已经打了半个星期了,我终于没纸了.
我该怎么做呢?
我发现了什么:
如果我们有加密的最后一步:
$FRUIT
FRUIT$
IT$FRU
RUIT$F
T$FRUI
UIT$FR
Run Code Online (Sandbox Code Playgroud)
我们可以知道原始字符串的第一个和最后一个字符,因为最右边的列是加密字符串本身,最左边的列总是按字母顺序排列.最后一个字符是加密字符串的第一个字符,因为$始终是字母表中的第一个字符,并且它只在字符串中存在一次.然后,如果我们从最右边的列中找到$字符,并在最左边的列中查找同一行上的字符,我们将得到第一个字符.
所以我们对加密字符串T $ UFIR的了解是原始字符串是F***T $,其中*是未知字符.
结束了我的想法.现在我必须利用全球网络并问另一个人:怎么样?
你可以说这是家庭作业,熟悉我的导师,我把我的赌注押在一个动态的编程问题上.
我有一个由非负整数组成的表,这些整数以这种方式排列:表中的每个元素都是不在其左侧或上方出现的最小值.这是一个6x6网格的示例:
0 1 2 3 4 5
1 0 3 2 5 4
2 3 0 1 6 7
3 2 1 0 7 6
4 5 6 7 0 1
5 4 7 6 1 0
Run Code Online (Sandbox Code Playgroud)
第一行和第一行以0 1 2 3 4 5开始...在坐标(x,x)中始终为0,如您所见.在此之后的每个图块上,您必须放置在同一行或列上尚不存在的最小正数.就像在数独拼图中一样:在同一行和列上不能有两次数字.
现在我必须在给定的坐标(y,x)中打印数字.例如[2,5] = 5
我提出了一个有效的解决方案,但它占用了太多的内存和时间,我只知道还有另一种方法.我的时间限制是1秒,我必须找到的坐标数最多可以达到(1000000,1000000).
这是我目前的代码:
#include <iostream>
#include <vector>
int main()
{
int y, x, grid_size;
std::vector< std::vector<int> > grid;
std::cin >> y >> x; // input the coordinates we're looking for
grid.resize(y, std::vector<int>(x, 0)); // resize …Run Code Online (Sandbox Code Playgroud) 我应如何深入描述算法的工作原理?
例如,我正在开发一个简单的俄罗斯方块游戏,我想解释一下如何旋转tetrominoes,这样的解释不能覆盖100行,但读者可以完全理解我在做什么。
我的算法的javadoc现在是:
/**
* Rotates the tetromino 90 degrees clockwise.
* <p>
* The rotating algorithm guarantees O(n^2) time complexity and O(1) space
* complexity, n being the width/height of the tetromino's layout.
* <p>
* The algorithm works by rotating each layer of the layout. A layer is
* rotated by choosing 4 elements, one from each of the layout's sides, and
* swapping them with each other in a clockwise manner. See an illustration
* …Run Code Online (Sandbox Code Playgroud) 我的程序有这个小问题。在 Visual Studio 2012 中它运行良好,但是如果我用 G++ 编译它(是的,由于我上面的原因,我必须使用它来编译),错误信号 11(SIGSEGV) 或 6(SIGABRT) 根据输入被触发. 这是一个编程练习,我有另一个程序(在在线服务器上)用 10 个不同的输入测试我的程序。正如我所说,该程序在使用 Visual Studio 2012 时编译并运行良好。
关于程序:它找到从起点(x,y)到多个出口的最短路径(出口数量无关且不同。可能只有1个出口,也可能有200个)。输入如下:
7 12 // maze height and width
##########.# //
#..........# //
#.###.###### //
#..X#.#..... // the maze blueprint
#.###.#.#### //
#..........# //
############ //
Run Code Online (Sandbox Code Playgroud)
还有我的程序:
#include <iostream>
#include <vector>
typedef struct _laby_t {
int h, w;
char **pohja; // 'pohja' is finnish and means layout
} laby_t;
typedef std::vector<int> monovector;
typedef std::vector< std::vector<int> > bivector;
laby_t *laby_allocate (int r, int c) …Run Code Online (Sandbox Code Playgroud)