BNU OJ 第26303 题 Touchscreen Keyboard

  BNU OJ26303Touchscreen Keyboard(题目链接)的解题报告。

  原题如下:

Touchscreen Keyboard

Problem Description

Nowadays, people do not use hardware keyboards but touchscreens. Usually, they touch on the wrong letters with their chunky fingers, because screen space is precious and the letters therefore too small.

Usually, a spell checker runs after typing a word and suggests other words to select the correct spelling from. Your job is to order that list so that more likely words are on top. The typical touchscreen keyboard looks like this:

qwertyuiop
asdfghjkl
zxcvbnm

You should use the distance between the letters to type a word: the distance is the sum of the horizontal and vertical distance between the typed and proposed letter. Assume you typed a w, the distance to e is 1, while the distance to z is 3.

The typed word and the list of words from the spell checker all have the same length. The distance between two words is the sum of the letter distances. So the distance between ifpv and icpc is 3.

Input

The first line of the input specifies the number of test cases t (0 < t < 20). Each test case starts with a string and an integer l on one line. The string gives the word that was typed using the touchscreen keyboard, while l specifies the number of entries in the spell checker list (0 < l ≤ 10). Then follow l lines, each with one word of the spell checker list. You may safely assume that all words of one test case have the same length and no word is longer than 10 000 characters (only lowercase 'a' - 'z'). Furthermore, each word appears exactly once in the spell checker list on one test case.

Output

For each test case, print the list of words sorted by their distance ascending. If two words have the same distance, sort them alphabetically. Print the distance of each word in the same line.

Sample Input

2
ifpv 3
iopc
icpc
gcpc
edc 5
wsx
edc
rfv
plm
qed

Sample Output

icpc 3
gcpc 7
iopc 7
edc 0
rfv 3
wsx 3
qed 4
plm 17

 

 

 

 

  先判断在键盘第几行,再判断在键盘的第几列。行差加上列差,即为所需误差,然后求和即可。

  C++语言代码如下:

/***
*
*                     Touchscreen Keyboard
*
*
*                                                  叶剑飞
*                                                    2012年10月3日
*
*******************************************************************************/#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cassert>using namespace std;const char * first = "qwertyuiop";
const char * second = "asdfghjkll";
const char * third = "zxcvbnm";typedef struct
{char word[10002];int sum;
} CORRECT;int PosInFirst( const char ch )
{for ( int i = 0 ; first[i] != '\0' ; i ++ ){if ( ch == first[i] )return i + 1;}return 0;
}int PosInSecond( const char ch )
{for ( int i = 0 ; second[i] != '\0' ; i ++ ){if ( ch == second[i] )return i + 1;}return 0;
}int PosInThird( const char ch )
{for ( int i = 0 ; third[i] != '\0' ; i ++ ){if ( ch == third[i] )return i + 1;}return 0;
}int FindError( const char a, const char b )
{int n;int pos_a, pos_b;if ( (pos_a = PosInFirst(a)) ){if ( (pos_b = PosInFirst(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;}else if ( (pos_b = PosInSecond(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;n ++;}else if ( (pos_b = PosInThird(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;n += 2;}}else if ( (pos_a = PosInSecond(a)) ){if ( (pos_b = PosInFirst(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;n ++;}else if ( (pos_b = PosInSecond(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;}else if ( (pos_b = PosInThird(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;n ++;}}else if ( (pos_a = PosInThird(a)) ){if ( (pos_b = PosInFirst(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;n += 2;}else if ( (pos_b = PosInSecond(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;n ++;}else if ( (pos_b = PosInThird(b)) ){n = pos_b - pos_a;if (n < 0 )n = -n;}}elseassert( false );return n;
}bool cmp( const CORRECT & a, const CORRECT & b )
{if ( a.sum == b.sum ){if ( strcmp( a.word, b.word ) < 0 )return true;elsereturn false;}elsereturn a.sum < b.sum;
}int main (void)
{int i, j;int n, m;char wrong[10002];CORRECT correct[16];scanf( "%d", &n );while ( n -- ){scanf( "%s%d", wrong, &m );for ( i = 0 ; i < m  ; i ++ ){correct[i].sum = 0;scanf( "%s", correct[i].word );for ( j = 0; correct[i].word[j] != '\0' ; j ++ )correct[i].sum += FindError( wrong[j], correct[i].word[j] );}sort( correct, correct+m, cmp );for ( i = 0 ; i < m  ; i ++ )printf( "%s %d\n", correct[i].word, correct[i].sum );}return 0;
}

转载于:https://www.cnblogs.com/yejianfei/archive/2012/10/04/2711215.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379237.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

列表(二)

1&#xff0c;什么是列表&#xff1f; 列表由一系列按特定顺序排列的元素组成。得知列表内的元素是有序的。 在Python中&#xff0c;用方括号&#xff08;[]&#xff09;来表示列表&#xff0c;并用逗号来分隔其中的元素。 color [red,blue,black,yellow]#定义一个字符串列表…

Zigbee在.Net Micro Framework系统中的应用

Zigbee是IEEE 802.15.4协议的代名词。根据这个协议规定的技术是一种短距离、低功耗的无线通信技术。这一名称来源于蜜蜂的八字舞&#xff0c;由于蜜蜂(bee)是靠飞翔和“嗡嗡”(zig)地抖动翅膀的“舞蹈”来与同伴传递花粉所在方位信息&#xff0c;也就是说蜜蜂依靠这样的方式构成…

ffmpeg-AVFrame分配内存问题

目录&#xff1a;1、格式&#xff1a;交错式2、格式&#xff1a;平坦式3、总结&#xff1a;1、格式&#xff1a;交错式 LRLRRLRLRLRLRLRLRLR 2、格式&#xff1a;平坦式 LLLLLLRRRRRR 3、总结&#xff1a; 两种方式的内存排列在AVFrame中分配是有区别的 交错式在一个buf…

stl中map函数_map :: empty()函数以及C ++ STL中的Example

stl中map函数C STL映射:: empty() (C STL map::empty()) It is built-in function in C STL and used to check whether the map container is empty or not i.e whether its size is 0 or not? 它是C STL中的内置函数&#xff0c;用于检查地图容器是否为空&#xff0c;即其…

C#使用Dotfuscator混淆代码以及加密

C#编写的代码如果不进行一定程度的混淆和加密&#xff0c;那么是非常容易被反编译进行破解的&#xff0c;特别是对于一些商业用途的C#软件来说&#xff0c;因为盯着的人多&#xff0c;更是极易被攻破。使用Dotfuscator可以实现混淆代码、变量名修改、字符串加密等功能。 这里介…

操作列表(三)

1&#xff0c;for循环(for 变量名 in 列表名:) phone [iphone 8, xiaomi10pro, huaweiv30pro, honor20, jianguopro]#定义一个列表phone for tel in phone:print("手机的类型为&#xff1a;" tel.title())#当然这里的每个元素也可以调用title()等一些方法 print(&…

C#特性之通俗演义

首先要说的是&#xff0c;可能一些刚接触C#的朋友常常容易把属性&#xff08;Property&#xff09;跟特性&#xff08;Attribute&#xff09;弄混淆&#xff0c;其实这是两种不同的东西。属性就是面向对象思想里所说的封装在类里面的数据字段&#xff0c;其形式为&#xff1a; …

栈应用_计算按运算符优先级分布的算式(代码、分析、汇编)

目录&#xff1a;代码&#xff1a;分析&#xff1a;汇编&#xff1a;代码&#xff1a; LinkList.h LinkList.c LinkStack.h LinkStack.c 栈-线性表 main.c #include <stdio.h> #include "LinkStack.h"//该程序用栈来计算算式 /*比如&#xff1a;1*56/(5-3)…

php globals_PHP $ GLOBALS(超级全局变量),带有示例

php globalsPHP $全球 (PHP $GLOBALS) PHP $GLOBALS is the only superglobal that does not begin with an underscore (_). It is an array that stores all the global scope variables. PHP $ GLOBALS是唯一不以下划线( _ )开头的超全局变量。 它是一个存储所有全局范围变量…

安装部署项目(转自)

1 新建安装部署项目 打开VS&#xff0c;点击新建项目&#xff0c;选择&#xff1a;其他项目类型->安装与部署->安装向导(安装项目也一样)&#xff0c;然后点击确定。 2 安装向导 关闭后打开安装向导&#xff0c;点击下一步&#xff0c;或者直接点击完成。 3 开始制作…

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

更改jdk&#xff0c;版本过高的缘故&#xff0c;更改jdk为1.7版本

kotlin 查找id_Kotlin程序查找给定范围内的素数

kotlin 查找idA prime number is a natural number that is greater than 1 and cannot be formed by multiplying two smaller natural numbers. 质数是大于1的自然数&#xff0c;不能通过将两个较小的自然数相乘而形成。 Given a range start and end, we have to print al…

socket代码

客户端:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> int main(int argc,char *argv[]) {int sockfd,numbytes;char buf[100];struct sockaddr_in th…

栈应用_将算式转成按运算符优先级分布(代码、分析、汇编)

目录&#xff1a;代码&#xff1a;分析&#xff1a;汇编&#xff1a;代码&#xff1a; LinkList.h LinkList.c LinkStack.h LinkStack.c 栈-线性表 main.c #include <stdio.h> #include "LinkStack.h"/* 该程序将 正常的算式 转换成按照运算符优先分布的算式…

课堂笔记(一)

1&#xff0c;怎样查询函数的用法 help(函数名) 2&#xff0c;表达式float(0b1100010101)float(0o1425)float(0x315)的结果是什么&#xff0c;并说明原因 True 浮点类型的数用二进制八进制十六进制的不同表达 3&#xff0c;oct()方法 转换八进制输出 4&#xff0c;hex()方…

Struts2.0标签使用之s:checkboxlist/

jsp代码如下&#xff1a; <s:form action"receive.action" method"post"> <s:checkboxlist id"user" name"cheuser" list"#request.userlist" listKey"id" listValue"name" lab…

[转]深入浅出Java设计模式之备忘录模式

本文转自&#xff1a;http://dev.yesky.com/450/2070450.shtml 一、引子   俗话说&#xff1a;世上难买后悔药。所以凡事讲究个“三思而后行”&#xff0c;但总常见有人做“痛心疾首”状&#xff1a;当初我要是……。如果真的有《大话西游》中能时光倒流的“月光宝盒”&#…

递归问题(代码、分析、汇编)

目录&#xff1a;代码&#xff1a;分析&#xff1a;汇编&#xff1a;代码&#xff1a; main.c #include <stdio.h>//该程序使用递归将字符串从后往前依次输出void reverse(char* s) {if( (s ! NULL) && (*s ! \0) ){reverse(s 1);printf("%c", *s);…

Java LocalDate类| ofYearDay()方法与示例

LocalDate类的YearDay()方法 (LocalDate Class ofYearDay() method) ofYearDay() method is available in java.time package. ofYearDay()方法在java.time包中可用。 ofYearDay() method is used to create an instance of LocalDate object that holds the value from the ye…

ASP.NET C#读写Cookie的方法!

Cookie (HttpCookie的实例)提供了一种在 Web 应用程序中存储用户特定信息的方法。例如&#xff0c;当用户访问您的站点时&#xff0c;您可以使用 Cookie 存储用户首选项或其他信息。当该用户再次访问您的网站时&#xff0c;应用程序便可以检索以前存储的信息。 创建Cookie方法…