A14755 | Linear Keyboard
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
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$ .
输入格式
The first line contains an integer $t$ ( $1 \leq t \leq 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.
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.
输出格式
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.
输入输出样例
输入 #1
5 abcdefghijklmnopqrstuvwxyz hello abcdefghijklmnopqrstuvwxyz i abcdefghijklmnopqrstuvwxyz codeforces qwertyuiopasdfghjklzxcvbnm qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qwertyuiopasdfghjklzxcvbnm abacaba
输出 #1
13 0 68 0 74
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted