4382. 快速打字

摘要
Title: 4382. 快速打字
Tag: 双指针
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

4382. 快速打字

  • 题意

    芭芭拉是一个速度打字员。
    为了检查她的打字速度,她进行了一个速度测试。
    测试内容是给定她一个字符串 I,她需要将字符串正确打出。
    但是,芭芭拉作为一个速度打字员,在追求速度的同时,难免会发生一些错误,按错一些按键。
    最终,芭芭拉打出的字符串为 P
    现在,芭芭拉想知道,能否仅通过删除一些额外字母的方式,将字符串 P变为字符串 I
    如果可以,则输出需要删除的字母数量,如果不行,则输出 IMPOSSIBLE。

  • 思路

    双指针,锁定I字符串的某一字符,遍历P的字符,遇到相同的再继续一起往下遍历,否则走到底,走出去了就是不可能

  • 代码

    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
    '''
    Author: NEFU AB-IN
    Date: 2023-07-08 21:41:05
    FilePath: \Acwing\4382\4382.py
    LastEditTime: 2023-07-09 00:24:30
    '''
    # import
    from sys import setrecursionlimit, stdin, stdout, exit
    from collections import Counter, deque
    from heapq import heapify, heappop, heappush, nlargest, nsmallest
    from bisect import bisect_left, bisect_right
    from datetime import datetime, timedelta
    from string import ascii_lowercase, ascii_uppercase
    from math import log, gcd, sqrt, fabs, ceil, floor


    class sa:
    def __init__(self, x, y):
    self.x = x
    self.y = y

    def __lt__(self, a):
    return self.x < a.x


    # Final
    N = int(1e5 + 10)
    M = 20
    INF = int(2e9)

    # Define
    setrecursionlimit(INF)
    input = lambda: stdin.readline().rstrip("\r\n") # Remove when Mutiple data
    read = lambda: map(int, input().split())
    AR = lambda x=0: [x] * N

    # —————————————————————Division line ——————————————————————

    t, = read()

    for _ in range(t):
    I = input()
    p = input()

    j, cnt, flag = 0, 0, 0
    for i in range(len(I)):
    while j < len(p) and I[i] != p[j]:
    j += 1
    cnt += 1
    if j >= len(p):
    flag = 1
    print(f"Case #{_ + 1}: IMPOSSIBLE")
    break
    if I[i] == p[j]:
    j += 1
    if flag == 0:
    print(f"Case #{_ + 1}: {cnt + len(p) - j}")
使用搜索:谷歌必应百度