日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BNU OJ 第26303 题 Touchscreen Keyboard

發布時間:2023/12/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的BNU OJ 第26303 题 Touchscreen Keyboard的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。