【HackerRank】Encryption
One classic method for composing secret messages is called a square code. The spaces are removed from the english text and the characters are written into a square (or rectangle). The width and height of the rectangle have the constraint,
floor(sqrt( len(word) )) <= width, height <= ceil(sqrt( len(word) ))
Among the possible squares, choose the one with the minimum area.
In case of a rectangle, the number of rows will always be smaller than the number of columns. For example, the sentence "if man was meant to stay on the ground god would have given us roots" is 54 characters long, so it is written in the form of a rectangle with 7 rows and 8 columns. Many more rectangles can accomodate these characters; choose the one with minimum area such that: length * width >= len(word)
ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots
The coded message is obtained by reading the characters in a column, inserting a space, and then moving on to the next column towards the right. For example, the message above is coded as:
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
You will be given a message in English with no spaces between the words.The maximum message length can be 81 characters. Print the encoded message.
Here are some more examples:
Sample Input:
haveaniceday
Sample Output:
hae and via ecy
题解:好像也在leetcode做过类似的题。
首先计算出编码后矩阵的行数和列数。行数=sqrt(string.length()),列数则要看字符串长度是不是完全平方数,如果是,就和行数相同,否则等于行数加1。然后遍历字符串,生成规定的串就可以了。这道题也没有必要多开一个二维数组空间,之间按照column的间隔取字符组成单词即可。
代码如下:
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String string = in.next();
int len = string.length();
int row = (int)Math.sqrt(len);
int column = row*row == len?row:row+1;
StringBuilder sb = new StringBuilder();
for(int i = 0;i < column;i ++){
for(int j = 0;i+j*column<len;j++)
sb.append(string.charAt(i+j*column));
sb.append(' ');
}
System.out.println(sb.toString());
}
}
【HackerRank】Encryption的更多相关文章
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【hackerrank】Week of Code 30
Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 【HackerRank】Coin on the Table
题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
- 【HackerRank】Cut the tree
题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...
- 【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...
随机推荐
- ntp集群时间同步
1. NTP 简介 网络时间协议(英语:Network Time Protocol,简称NTP)是在数据网络潜伏时间可变的计算机系统之间通过分组交换进行时钟同步的一个网络协议.自1985年以来,NTP ...
- 配置LANMP环境(1)-- 安装虚拟机VMware与安装CentOS7.2系统
一.安装虚拟机VMware 1.下载VMware傻瓜式安装 2.配置 二.安装CentOS7.2系统 镜像下载链接 1.放入ios镜像文件之后,启动虚拟机,选择7.0+的 ...
- 通过Get方式传递数据
1:因为get传参数有个特点就是不能超过256字节.如果数据大的话会溢出. 解决办法: $data=json_encode($data_array); 然后在拼接超链接: <a href=&qu ...
- TP view中跳转到某个控制器
#直接用 __MODULE__/控制器名/方法名/参数名/参数 <a href="__MODULE__/Product/list_table/goods_id/<?php ech ...
- oracle 启动模式
转载自:http://blog.csdn.net/nsj820/article/details/6573525 <一>.ORACLE数据库启动模式 1.启动SQL*PLUS不与数据库连接 ...
- Marple表演电影字幕
119501:15:59,702 --> 01:16:02,782我的幸运死了 而我很清楚是谁杀了她的 (格雷格)My Lucky is dead, and I know perfectly w ...
- 水仙花数-python
题目: 求999以内的水仙花数? 分析: 如果一个3位数等于其各位数字的立方和,则称这个数为水仙花数. 例如:1^3 + 5^3+ 3^3 = 153,因此153就是一个水仙花数 我们需计算出153的 ...
- ios -过滤字符串特殊字符
//过滤 " [ ] " + (NSString *)onTwoCommseparatedWithString:(NSString *)string { //过滤 " [ ...
- go语言获取字符串元素的个数
1:获取字符串字节的个数,并按字节挨个输出 package main import ( "fmt" ) func main() { var str string = "a ...
- phpcs,phpmd,phan安装部署,phpstorm配置phpunit
git参考地址:https://github.com/YunhanTech/overview/blob/master/php/learn-road.md phpcs 安装 composer globa ...