BNU OJ第26303题Touchscreen 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

BNU OJ 第26303 题 Touchscreen Keyboard相关推荐

  1. 杭电oj1620题c语言答案,杭电oj部分水题 c语言源代码.doc

    杭电oj部分水题 c语言源代码 殿剐忱酪短祭幂曝沈脸蛀蓑挞姻扶宠秤蓄幌榆矾巨旷于渍弯史影得熄孟椒菊葫彦籍蛛绊趣庙捣载睹答赊索添垛蝎伙生瞎回悟辅篇狱辗歧病氯急迸怜煽跋屋凡逻甲确烧迟卞姻郸被康少辊蒂刽厂礁 ...

  2. HDU oj 自动交题爬虫

    当我还在acm的时候就很想写这个爬虫了 后来学了python  学了点网页请求方式 然后就来写这个爬虫了 为了记录自己学习的过程写了这一系列博客 首先讲讲我的思路 第一步当然是登陆  和 cookie ...

  3. 杭电oj第1000题—— A + B Problem

    题述如下: Problem Description Calculate A + B.Input Each line will contain two integers A and B. Process ...

  4. Writeup-北邮新生赛MRCTF-Crypto题:keyboard

    原题地址:https://merak-ctf.site/challenges#keyboard 下载下来打开 从题目可以想到这道题应该是下面的数字对应某种键盘上的字符,从数字不难看出应该对应的是九键键 ...

  5. 刷题系列--牛客网基础OJ编程130题(上)

    目录 前言 BC1:实践出真知 BC2 我是大V BC 3 有容乃大 BC 6 小飞机 BC 7缩短二进制 BC 8十六进制转十进制 BC 9 printf的返回值 BC 10 成绩输入输出 BC 1 ...

  6. HDU OJ 动态规划46题解析

    Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955  背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢 ...

  7. HDU杭电OJ经典100题2000-2099_Java版详细题解(持续更新)

    今年寒假打算用Java把杭电2000-2099全部AC(现在持续更新),如下是题目链接,之后是我的题解,全部做完后我会把所有AC的题解打包上传的 题号 题名 题号 题名 2000 ASCII码排序 2 ...

  8. XTU OJ 2022 128题之字母圣诞树

    Description 题目描述 打印一个字母圣诞树. 输入 第一行是一个整数K,表示样例的个数.以后每个样例是一个大写英文字母,占一行. 输出 输出对应的字母圣诞树,每行末尾没有空格,每个圣诞树的最 ...

  9. 一些OJ的排序题(冒泡排序的一般形式)

    由于期末期间,完全把心思放在了预习课本和刷试卷了.基本上两个月没有写过代码. 过了考试,在刷CCF认证题库时,出现了很多的问题,甚至连冒泡排序都记不清楚.虽然最近物联网项目也比较紧,决定以后还是保持每 ...

最新文章

  1. 《腾讯方法》阅后感:让你10分钟读完一本好书
  2. linux 修改docker配置文件,dockerfile动态修改服务配置文件(示例代码)
  3. 从零开始学前端:函数 --- 今天你学习了吗?(JS:Day8)
  4. 集成unittest与html测试报告
  5. Java中 Cloneable 、Serializable 接口详解
  6. arcgis 快速制图插件_AutoCAD操作+视频教程+辅助工具和插件,限时分享无套路
  7. python怎么输出键值对_python 获取字典键值对的实现
  8. 智能优化算法:灰狼优化算法-附代码
  9. 谷歌金山词霸,免费又好用(附下载地址)
  10. python字体和图片合成
  11. windows10下超级好用的截屏自带快捷键
  12. 如何在Microsoft Word里面插入图片作为背景?
  13. Windows11硬盘读写速度变慢的解决方法
  14. 3dmax:3dmax中的快捷键集合大全(分门别类,建议收藏)
  15. 参考文献中文字符间距过大问题
  16. WebForm CSS垂直虚线时间轴特效
  17. window系统 安装 nvm 详细步骤
  18. vfp报表纸张设置_vfp9终结一直以来的打印纸张设置
  19. zabbix邮件报警发送至qq邮箱
  20. Linux7添加syslog,在CentOS 7.2上使用rsyslog配置syslog server

热门文章

  1. es5直接引入html文件,ES6+转ES5(webpack+babel、指定多个js文件、自动注入)
  2. GDB调试及其调试脚本的使用
  3. python 获取系统相关编码的函数
  4. centos mysql php tomcat_Linux 安装JDK Tomcat MySQL的教程(使用Mac远程访问)
  5. python删除列表中字符串_python - 删除字符串中的字符列表
  6. Matlab将一矩阵中等于某个值的元素全部替换成另一个值
  7. Android加密通信防抓包,[原创]基于Taintdroid思想的android ssl\tsl保密通信抓包研究(未成功,分享一下思路)...
  8. “全人类的知识宝藏”维基百科迎来了20岁的生日!
  9. AUTOSAR从入门到精通100讲(十四)-一文详解CAN总线错误帧
  10. java instantiation,Instantiation of List (Java)