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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 为Eclipse指定Java VM[通俗易懂]

    重装系统后,Eclipse无法启动,说找不到JavaVM.   网上搜了一下,解决方法如下:   创建快捷方式,右键快捷方式,打开属性.在目标处输入” -vm你的Java根目录\jre\bin\javaw.exe”.然后用快捷方式打开就可以了.

    2022年4月12日
    106
  • sublimetext3没有package control_mvn install

    sublimetext3没有package control_mvn installsublime3 Package Control: install Package 超时

    2022年4月21日
    43
  • 如果SQL Server 配置管理器没有找到就代表安装失败?

    如果SQL Server 配置管理器没有找到就代表安装失败?首先,只要你安装的方法正确,就不要有安装失败或安装不完全的想法(或许安装时没有勾选配置管理工具),不要立马重装或者修复或其他操作。因为我之前因为重装电脑,安装过无数次,就有一次没有找到,在网上找解决方法,一律是“证明你安装不完全”、“重装,安装有问题”之类的回答,看完立马就泻气,但不要这样,浪费时间还不一定会解决问题。解决方法:1、一般安装完SQLServer2008或其他版本,在“

    2022年7月20日
    17
  • 闭包概念及面试题

    闭包概念及面试题如何产生闭包(closure)闭包(closure),是指函数变量可以保存在函数作用域内,因此看起来是函数将变量“包裹”了起来。//根据定义,包含变量的函数就是闭包也就是函数嵌套函数就可以称之为闭包.作用域应对的特殊情况,有两种表现:函数作为参数被传递函数作为返回值被带回函数中的自由变量,取决于函数定义的地方,跟执行的地方没关系闭包的应用场景闭包应用场景1,封装对象的私有属性和方法隐藏数据做一个简单的缓存工具//闭包隐藏数据,只提供APIfunctioncreat

    2022年10月9日
    4
  • python自动补全设置_python代码补全

    python自动补全设置_python代码补全Hello,大家好,我是橘子呀~前几期橘子给大家介绍了Python是什么以及如何安装Python软件。也分享了如何系统的以及高效率的去学习Python这门课程,还有一些避雷建议,今天橘子就分享给大伙一点宝藏干货!!!藏不住的东西,不如扬了它!请大家把它收入囊中~不要辜负我的一片苦心~一、Pycharm常用快捷键(Windows)Ctrl+Q快速查看文档Ctrl+鼠标左键简介及代码定义…

    2022年8月26日
    8
  • Java8(1):当 Lambda 遇上受检异常[通俗易懂]

    Java8(1):当 Lambda 遇上受检异常

    2022年2月23日
    51

发表回复

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

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