CF1860E.Fast Travel Text Editor

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a string ss, consisting of lowercase Latin letters.

There's a cursor, initially placed between two adjacent letters of the string. The cursor can't be placed before the first or after the last letter.

In one move, you can perform one of three actions:

  • move a cursor one position to the left (if that doesn't place it before the first letter);
  • move a cursor one position to the right (if that doesn't place it after the last letter);
  • let xx be the letter immediately to the left of the cursor, and yy be the letter immediately to the right of the cursor. Choose any pair of adjacent letters in the string such that the left of them is xx and the right of them is yy, and move the cursor to the position between these two letters.

You are asked mm queries. In each query, you are given two integers ff and tt, which correspond to two valid positions of the cursor in the string. In response to the query, you have to calculate the minimum number of operations required to move the cursor from position ff to position tt.

给你一个由小写拉丁字母组成的字符串 ss。

有一个光标,初始时位于字符串中两个相邻字母之间。光标不能置于第一个字母之前或最后一个字母之后。

每次操作可以执行以下三种动作之一:

  • 将光标向左移动一个位置(前提是移动后不会置于第一个字母之前);
  • 将光标向右移动一个位置(前提是移动后不会置于最后一个字母之后);
  • 设光标左侧紧邻的字母为 xx,右侧紧邻的字母为 yy;在字符串中任选一对相邻字母,使得其左侧字母为 xx、右侧字母为 yy,然后将光标移至这对相邻字母之间。

你需要处理 mm 个查询。每个查询给出两个整数 ff 和 tt,分别表示字符串中两个合法的光标位置。对于每个查询,你需要计算将光标从位置 ff 移动到位置 tt 所需的最少操作次数。

输入格式

The first line contains a string ss (2≤∣s∣≤5⋅1042 \le |s| \le 5 \cdot 10^4), consisting of lowercase Latin letters.

The second line contains a single integer mm (1≤m≤5⋅1041 \le m \le 5 \cdot 10^4) — the number of queries.

The ii-th of the next mm lines contains two integers ff and tt (1≤f,t≤∣s∣−11 \le f, t \le |s| - 1) — the initial and the target positions of the cursor in the ii-th query. Position xx means that the cursor is between the xx-th and the (x+1)(x+1)-st letters of the string (11-indexed).

第一行包含一个字符串 ss(2≤∣s∣≤5⋅1042 \le |s| \le 5 \cdot 10^4),由小写拉丁字母组成。

第二行包含一个整数 mm(1≤m≤5⋅1041 \le m \le 5 \cdot 10^4)—— 查询次数。

接下来的 mm 行中,第 ii 行包含两个整数 ff 和 tt(1≤f,t≤∣s∣−11 \le f, t \le |s| - 1)—— 第 ii 次查询中光标的初始位置和目标位置。位置 xx 表示光标位于字符串中第 xx 个与第 (x+1)(x+1) 个字母之间(位置下标从 11 开始)。

输出格式

For each query, print the minimum number of actions required to move the cursor from position ff to position tt.

对于每个查询,输出将光标从位置 ff 移动到位置 tt 所需的最少操作次数。

输入输出样例

  • 输入#1

    codecode
    3
    1 7
    3 5
    3 6

    输出#1

    3
    2
    2
  • 输入#2

    abcdefghij
    3
    1 9
    9 1
    5 5

    输出#2

    8
    8
    0
  • 输入#3

    aaaaaaaaaaaa
    4
    10 8
    3 7
    6 1
    11 11

    输出#3

    1
    1
    1
    0

输入解题思路,AI测评打分。不知道怎么写?

首页