CF1607A.Linear Keyboard
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a keyboard that consists of 26 keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.
You have to type the word s on this keyboard. It also consists only of lowercase Latin letters.
To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.
Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.
For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions 8, 5, 12 and 15, respectively. Therefore, it will take ∣5−8∣+∣12−5∣+∣12−12∣+∣15−12∣=13 units of time to type the word "hello".
Determine how long it will take to print the word s.
你有一个包含 26 个按键的键盘。这些按键按某种顺序从左到右依次排列在一行中,每个按键对应一个唯一的小写拉丁字母。
你需要在此键盘上输入单词 s,该单词也仅由小写拉丁字母组成。
要输入一个单词,你需要依次连续地输入其中每一个字母。输入每个字母时,必须将手准确地放在对应按键的正上方并按下它。
手在不同按键之间移动需要时间,该时间等于这两个按键位置编号之差的绝对值(按键从左到右编号为 1,2,…,26)。按下按键本身不消耗时间,且将手放置在单词第一个字母对应的按键上也不消耗时间。
例如,考虑一个按键按字母表顺序('a' 到 'z')依次排列的键盘。此时字母 'h'、'e'、'l' 和 'o' 分别位于位置 8、5、12 和 15。因此,输入单词 "hello" 所需时间为
∣5−8∣+∣12−5∣+∣12−12∣+∣15−12∣=13
个时间单位。
请计算输入单词 s 所需的总时间。
输入格式
The first line contains an integer t (1≤t≤1000) — the number of test cases.
The next 2t lines contain descriptions of the test cases.
The first line of a description contains a keyboard — a string of length 26, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard.
The second line of the description contains the word s. The word has a length from 1 to 50 letters inclusive and consists of lowercase Latin letters.
第一行包含一个整数 t(1≤t≤1000)—— 表示测试用例的数量。
接下来的 2t 行包含各测试用例的描述。
每个测试用例的第一行包含一个键盘 —— 一个长度为 26 的字符串,仅由小写拉丁字母组成。从 'a' 到 'z' 的每个字母在键盘上恰好出现一次。
每个测试用例的第二行包含单词 s。该单词的长度为 1 至 50 个字母(含端点),且仅由小写拉丁字母组成。
输出格式
Print t lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word s on the given keyboard.
输出 t 行,每行包含对应测试用例的答案。测试用例的答案即在给定键盘上输入单词 s 所需的最少时间。
输入输出样例
输入#1
5 abcdefghijklmnopqrstuvwxyz hello abcdefghijklmnopqrstuvwxyz i abcdefghijklmnopqrstuvwxyz codeforces qwertyuiopasdfghjklzxcvbnm qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qwertyuiopasdfghjklzxcvbnm abacaba
输出#1
13 0 68 0 74
输入解题思路,AI测评打分。不知道怎么写?