22练习赛

文章提供了两个编程问题:一是关于在特定键盘布局上输入单词的最短时间计算,涉及字符串处理和最优化;二是关于奇数蚱蜢的移动模式,涉及到数学规律和模运算。第三个问题是数组中最小元素提取的优化。这些问题都常见于算法竞赛或编程面试中。
摘要由CSDN通过智能技术生成

A - Linear Keyboard

You are given a keyboard that consists of 2626 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 ss 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 88, 55, 1212 and 1515, 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”.

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

您将得到一个由2626个键组成的键盘。按键按一定的顺序依次排列在一行中。每个键对应一个唯一的小写拉丁字母。
你必须在这个键盘上键入ss这个词。它也只由小写拉丁字母组成。
要键入一个单词,你需要一个接一个地连续键入它的所有字母。要键入每个字母,您必须将手准确地放在相应的键上并按下它。
在键之间移动手需要的时间等于这些键的位置之间的差的绝对值(键从左到右编号)。没有时间花在按键和把手放在单词的第一个字母上。
例如,考虑一个键盘,其中从“a”到“z”的字母按连续的字母顺序排列。字母“h”、“e”、“l”和“o”分别位于位置88、55、1212和1515。因此,键入“你好”一词需要|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13∣5−8∣+∣12−5∣+∣12−12∣+∣15−12∣=13
确定打印单词ss需要多长时间。

Input
The first line contains an integer tt (1 \leq t \leq 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.

第一行包含一个整数tt(1\leq t\leq 10001≤t≤1000)-测试用例的数量。
接下来的2t行包含测试用例的描述。
描述的第一行包含一个键盘——一个长度为2626的字符串,仅由小写拉丁字母组成。从“a”到“z”的每个字母在键盘上只出现一次。
描述的第二行包含单词ss。这个单词的长度从11个字母到5050个字母,由小写拉丁字母组成。

Output
Print tt 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 ss on the given keyboard.

Sample 1
Input
5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba
Output
13
0
68
0
74

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;cin>>n;
    while(n--)
    {
        vector<int>v;
        string s;cin>>s;//读入26个
        string a;cin>>a;//读入字符串
        for(int i=0;i<a.size();i++)
        {
            for(int j=0;j<s.size();j++)
            {
                if(a[i]==s[j])
                {v.push_back(j+1);}
            }
        }
        if(v.size()==1)//只打印一个字母
        {cout<<0<<endl;continue;}
        int count=0;
        for(int i=0;i<v.size()-1;i++)
        {
            count+=fabs(v[i+1]-v[i]);
        }
        cout<<count<<endl;
    }
    return 0;
}

B - Odd Grasshopper

在这里插入图片描述

在这里插入图片描述
输入
9
0 1
0 2
10 10
10 99
177 13
10000000000 987654321
-433494437 87178291199
1 0
-1 1

输出
-1
1
11
110
190
9012345679
-87611785637
1
0

找规律,偶数时,若移动次数%4为0,则位置=初始位置;若移动次数%4为1,则位置=初始位置-移动次数;若移动次数%4为2,则位置=初始位置+1;若移动次数%4为3,则位置=初始位置+(移动次数+1)
奇数时,加减与偶数时相反

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,x,i,count;cin>>n;//x:初始位置 i:移动次数
    while(n--)
    {
        count=0;
        cin>>x>>i;
        count += x;
        if(x%2==0)//初始坐标为偶数
        {
            if (i % 4 == 0) count += 0;
            else if (i % 4 == 1) count -= i;
            else if (i % 4 == 2) count += 1;
            else if (i % 4 == 3) count += (i + 1);
        }
        else //初始坐标为奇数
        {
            if (i % 4 == 0) count += 0;
            else if (i % 4 == 1) count += i;
            else if (i % 4 == 2) count -= 1;
            else if (i % 4 == 3) count -= (i + 1);
        }
        cout<<count<<endl;
    }
    return 0;

C - Minimum Extraction

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;int a[n];
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        if(n==1)//若只有一个数,那最小元素的最大值就是他本身
            cout<<a[0]<<endl;
        else
        {
            sort(a,a+n,greater<int>());//从大到小排序
            int max_=a[n-1];//记录下最小元素
            for(int j=n-1;j>0;j--)
            {
                max_=max(max_,a[j-1]-a[j]);//最小元素与另外的最小元素作比较。最后找出最大值
            }
            cout<<max_<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值