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


相关推荐

  • dfs是什么意思_英语单词搜索软件

    dfs是什么意思_英语单词搜索软件给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词。单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。示例 1:输入:board = [[“o”,”a”,”a”,”n”],[“e”,”t”,”a”,”e”],[“i”,”h”,”k”,”r”],[“i”,”f”,”l”,”v”]], words = [“oath”,

    2022年8月8日
    3
  • CompareTo Semantics[通俗易懂]

    CompareTo Semantics[通俗易懂]ForthepropertytypesotherthanBOOLEAN,NAME,PATHandBINARY,comparisonrelationsaredefinedintermsoftheresultofthecompareTomethodoninstancesV1andV2oftheJavaclasscorrespondingto

    2022年7月15日
    15
  • jdk14下载与安装教程(win10)超详细

    jdk14下载与安装教程(win10)超详细一、前言现在jdk已经升级到JDK14版本了,这里也记录一下jdk14的下载及安装过程,对于刚学习java的小伙伴可以参考,熟手可忽略,呵呵~二、下载安装步骤一、首先是去jdk官网下载,官网下载还需要注册,如果大家不怕麻烦的话可以去官网下载,下载速度也是龟速,我也是花了好长时间才下载下来,大家可以点击我的网盘目录jdk目录下载,目录也有其它低版本的,如果有需要大家根据需要自行选择。…

    2022年5月26日
    187
  • java动态代理中的invoke方法是如何被自动调用的「建议收藏」

    java动态代理中的invoke方法是如何被自动调用的「建议收藏」Java中动态代理的实现,关键就是这两个东西:Proxy、InvocationHandler,下面从InvocationHandler接口中的invoke方法入手,简单说明一下Java如何实现动态代理的。        首先,invoke方法的完整形式如下: Java代码  public Object invoke(Object proxy, Method m

    2022年4月30日
    119
  • pycharm2021.2激活码【中文破解版】

    (pycharm2021.2激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS32PGH0SQB-eyJsaWN…

    2022年3月26日
    71
  • lstm怎么预测长时间序列_时间序列预测代码

    lstm怎么预测长时间序列_时间序列预测代码写在前面LSTM模型的一个常见用途是对长时间序列数据进行学习预测,例如得到了某商品前一年的日销量数据,我们可以用LSTM模型来预测未来一段时间内该商品的销量。但对于不熟悉神经网络或者对没有了解过RNN模型的人来说,想要看懂LSTM模型的原理是非常困难的,但有些时候我们不得不快速上手搭建一个LSTM模型来完成预测任务。下面我将对一个真实的时间序列数据集进行LSTM模型的搭建,不加入很多复杂的功能,快速的完成数据预测功能。问题大概如下:某煤矿有一个监测井,我们每20分钟获…

    2022年9月10日
    0

发表回复

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

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