问题翻译:

You are given a keyboard that consists of 26 keys. The keys are arranged sequentially(按顺序)

有一个26个键排成一行(从左到右依次代表小写字母a-z)的键盘

You have to type the word s  on this keyboard. It also consists only of lowercase Latin letters.

用这个键盘敲出单词s,s都由小写字母表示

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(相应的)

为了敲出单词,必须连续敲出a-z所有字母

为了敲出字母,必须把手移动到相应位置并且按压它

Moving the hand between the keys takes time which is equal to the absolute value(绝对值) of the difference(差)

移动的时间等于两个位置的距离差值,不计手放在第一个字母的时间和按下按键的时间

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|5−8|+|12−5|+|12−12|+|15−12|=13 units of time to type the word "hello".

例:hello 在 8,5,12,12,15

所用时间为 8-5  + 12-5  +12- 12 +15-12 =13

结果为13

Determine how long it will take to print the word s.

问题:求出敲出单词所用时间

Input

The first line contains an integer tt (1≤t≤10001≤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 2626, 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 ss. The word has a length from 11 to 5050 letters inclusive and consists of lowercase Latin letters.

第一行为例子个数

下面二行为一组,每组第一行是键盘顺序(长度为26且a-z只出现一次)

每组第二行是要敲出的单词

Output

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.

输出:

每个case所需时间

问题概述:

给定S1包含a-z26个小写字母

给定s2,求s2中每个字符和下一个字符,它们两个s1下标差的绝对值之和

思路:
用map记键盘上每一个字母对应下标

遍历word,map查找下标计算出时间

代码:

#include<iostream>
#include<unordered_map>
#include<vector>
#include<string>
using namespace std;
long long  type(string s1, string s2)
{
	unordered_map<char, int>board;
	for (int i = 0; i < s1.size(); i++)
	{
		board[s1[i]] = i;
	}
	long long time = 0;
	long long e1, e2;
	if (s2.size() == 1)
	{
		return 0;
	}
	for (int i = 0; i < s2.size()-1; i++)
	{
		e1 = board[s2[i]];
		e2 = board[s2[i + 1]];
		if (e1 >= e2)
		{
			time += e1 - e2;
		}
		else
		{
			time += e2 - e1;
		}
	}
	return time;
}
int main()
{
	long long m;
	cin >> m;
	string s1;
	string s2;
	vector<vector<string>>vec;
	vector<string>temp;
	while (m--)
	{
		cin >> s1;
		cin >> s2;
		temp.push_back(s1);
		temp.push_back(s2);
		vec.push_back(temp);
		temp.clear();
	}
	for (long long i = 0; i < vec.size(); i++)
	{
		s1 = vec[i][0];
		s2 = vec[i][1];
		cout << type(s1, s2) << endl;
	}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.