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.pswp.cn/news/379237.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379237.shtml
英文地址,請注明出處:http://en.pswp.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方法…