Optimal Keypad[通俗易懂]

Optimal Keypad[通俗易懂]Description OptimusMobilesproducesmobilephonesthatsupportSMSmessages.TheMobileshaveakeypadof12keys,numbered1to12.Thereisacharacterstringassignedtoeachkey.Totypeinthe

大家好,又见面了,我是你们的朋友全栈君。

Description 
Optimus Mobiles produces mobile phones that support SMS messages. The Mobiles have a keypad of 12 keys, numbered 1 to 12. There is a character string assigned to each key. To type in the n-th character in the character string of a particular key, one should press the key n times. Optimus Mobiles wishes to solve the problem of assigning character strings to the keys such that for typing a random text out of a dictionary of common words, the average typing effort (i.e. the average number of keystrokes) is minimal. 


Optimal Keypad[通俗易懂]
Figure 1



To be more precise, consider a set of characters {a, b, c,…, z, +, *, /, ?} printed on a label tape as in Fig. 2. We want to cut the tape into 12 pieces each containing one or more characters. The 12 labels are numbered 1 to 12 from left to right and will be assigned to the keypad keys in that order. 


Optimal Keypad[通俗易懂]

Figure 2


You are to write a program to find the 11 cutting positions for a given dictionary of common words. The cutting positions should minimize the average number of keystrokes over all common words in the dictionary. Your output should be a string of 11 characters, where character i in this string is the first character of the (i+1)
th label.


Input 
The first line contains a single integer t (1 <= t <= 10), the number of test cases. Each test case starts with a line, containing an integer M (1 <= M <= 10000), the number of common words in the test case. In each M subsequent line, there is a common word. Each common word contains at most 30 characters from the alphabet {a, b, c,…, z, +, *, /, ?}.

Output 
The output contains one line per test case containing an optimal cut string. Obviously, there may be more than a single optimal cut string, so print the optimal cut string which is the smallest one in lexicographic order.

Sample Input 

2
2
hi
ok
5
hello
bye
how
when
who


Sample Output 

bcdefghijko
bcdefhlnowy


Source 

/**题目大意:给出“abcd...z+* /?"的序列,要求分为12段,作为手机的12个按键上的字符,使得我们用手机输入单词时按键的次数最少,单词的数量是10000每个长度最大为30。 思路:注意到输入的顺序可以打乱,即输入“ab”和输入“ba”花费是一样的,我们可以预处理一下,统计这30个字符出现的次数,现在我们要做的就是把这 30个字符分成12份。容易想到的方程是 dp[i][j] = min{ dp[i - 1][k - 1] + sum(k, j) }; dp[i][j]表示前j个字符分成i份,sum(k, j)表示第k个字符到第j个字符划分在同一个按键内的花费;最后记录一下路径。 **/ 
#include <iostream> #include <cstring> #include <cstdio> using namespace std;const int N = 32;char alph[] = " abcdefghijklmnopqrstuvwxyz+*/?";int flect[140];int num[N];int s[N][N];int dp[N][N];int ans[N];void init(){ 	for (int i = 1; i <= 30; i++)		flect[(int) alph[i]] = i;} char str[N];int main(){ 	//freopen("in.txt", "r", stdin);	init();	int n, T, i, j, k;	scanf("%d", &T);	while (T--)	{		scanf("%d", &n);		memset(num, 0, sizeof(num));		for (i = 0; i < n; i++)		{			scanf("%s", str);			for (j = 0; str[j]; j++)				num[flect[(int)str[j]]]++;		}		//dp		memset(dp, 0x3f, sizeof(dp));		int sum = 0;		for (i = 1; i <= 19; i++)		{			sum += num[i] * i;			dp[1][i] = sum;			s[1][i] = 1;		}		for (i = 2; i <= 12; i++)			for (j = i; j <= 30; j++)			{				for (k = i; k <= j; k++)				{					sum = 0;					for (int h = k; h <= j; h++)						sum += num[h] *(h - k + 1);					if (dp[i - 1][k - 1] + sum < dp[i][j])					{						dp[i][j] = dp[i - 1][k - 1] + sum;						s[i][j] = k;					}				}			}			int cnt = 0;			int now = 30;			for (i = 12; i > 1; i--)			{				ans[cnt++] = s[i][now];				now = s[i][now] - 1;			}			for (i = cnt - 1; i >= 0; i--)				printf("%c", alph[ans[i]]);			printf("\n");	}	} 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130633.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月28日 下午11:20
下一篇 2022年4月28日 下午11:40


相关推荐

  • 2060s

    2060s2060s老版本三星核心:175显存:2030功耗:125算力:43+镁光核心:-400显存:1050功耗:125算力:37+升级系统后三星核心:1035显存:2030镁光核心:1035显存:1050

    2022年6月22日
    67
  • docker的端口映射_外网远程桌面端口映射

    docker的端口映射_外网远程桌面端口映射Docker端口映射实现网络访问首先,大家如果看到有什么不懂的地方,欢迎吐槽!!!我会在当天或者第二天及时回复,并且改进~~Docker运行容器之后却发现没IP,没端口,那要如何访问容器呢?下面我来介绍下Docker通过端口映射来实现网络访问一、从外部访问容器应用在启动容器的时候,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数指定端口映射。先来说说p和P吧-p可以指定要映射的端口,并

    2022年10月9日
    6
  • rocketmq负载均衡机制_rocketmq topic

    rocketmq负载均衡机制_rocketmq topicProducer发送消息时,会首先获取Topic路由信息(通过本地+注册中心拉取),RocketMQ的架构里有多个Broker服务器,而消息队列也会存在于多个Broker服务器里,所以就需要负载均衡策略来将流量尽可能均匀的打到所有服务器上。本章节就介绍一下RocketMQ中常用的四种负载均衡策略。找到Producer发送消息时选择消息队列的逻辑,在类中定义了方法:进入到方法里:上述代码的类中定义了方法:根据源码可以很清楚地看到,默认策略就是依次选择消息队列进行发送,具体的执行细节如下:如何选一个

    2022年10月13日
    3
  • PhpStorm中报 “Cannot run program git.exe, 系统找不到指定的文件” 

    PhpStorm中报 “Cannot run program git.exe, 系统找不到指定的文件” 

    2021年10月10日
    47
  • 自己动手写游戏:Flappy Bird

    一、关于FlappyBird《FlappyBird》是由来自越南的独立游戏开发者DongNguyen所开发的作品,游戏中玩家必须控制一只小鸟,跨越由各种不同长度水管所组成的障碍,而这只鸟其实是

    2021年12月19日
    45
  • 【手把手教你树莓派3 (三)】scp命令传文件

    【手把手教你树莓派3 (三)】scp命令传文件概述在没有显示器的情况下,只能通过ssh来远程登录树莓派。那比如我们要在树莓派里面建站,绝对不会想通过树莓派的终端coding,其实最好的办法是在另一台linux机器下编好代码,然后把项目拷贝至树莓派。scp命令可以使用scp命令,这个命令是cp命令的远程版。如果从本机传文件到树莓派,我们需要另开一个本机的终端(而非远程ssh连接树莓派的)命令如下:scplocal_file

    2022年8月22日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号