Linear Keyboard(模拟):map容器模拟

108 篇文章 0 订阅

Linear Keyboard

题面翻译

给定一个字符串 s s s,保证字符串 s s s 只包含小写字母,现在你要用一个奇怪的键盘来打这一个字符串 s s s

假设键盘的排列顺序是一条 s 2 s_2 s2 的直线。

第一个字母你不需要花费任何时间打字,然后打后面的每一个字母 s i s_i si 花费的时间是 d i s t ( s i , s i − 1 ) dist(s_i, s_{i - 1}) dist(si,si1)

输出最后需要花费的时间。

d i s t ( a , b ) = ∣ a − b ∣ dist(a, b) = |a - b| dist(a,b)=ab

其中字符 a , b a,b a,b 代表在键盘 s 2 s_2 s2 中, a a a b b b 的位置相隔的距离。

题目描述

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 $ .

输入格式

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.

输出格式

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

样例输入 #1

5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba

样例输出 #1

13
0
68
0
74

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>

using namespace std;

string c,b;
map<char,int>a;
int n;

int main(){
    cin>>n;
    
    for(int i=1;i<=n;i++){
        a.clear();
        cin>>c>>b;
        for(int i=0;i<c.size();i++){
            a[c[i]]=i;
        }
        int ans=0;
        for(int i=1;i<b.size();i++){
            ans+=abs(a[b[i]]-a[b[i-1]]);
        }
        
        cout<<ans<<endl;
        
    }
    
    return 0;
}
  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值